| Author |
Topic |
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2012-01-08 : 15:49:23
|
| HI,I need the query for belowI have data from flat file and i want to save the code column without 0 beforecolumnscode nvarchar(7) name nvarchar(40)----------flat file----------EmpCode name003 Raj048 Kumar073 selvam100 gopal140 gomathiT3R NayakiYEU YasodhaOPE Easwari063 RaniI want to insert removing 0 before code column----------SQL Insert----------EmpCode Name3 Raj48 Kumar73 selvam100 gopal140 gomathiT3R NayakiYEU YasodhaOPE Easwari63 RaniKindly help me with queryI tried below and not working :( LTRIM(EmpCode,'0'); (CAST (CAST EmpCode AS int) AS varchar(7)) THANKSSHANMUGARAJnshanmugaraj@gmail.com |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-01-08 : 15:56:40
|
| CASE WHEN ISNUMERIC(EmpCode) THEN CAST (CAST EmpCode AS int) AS varchar(7) ELSE EmpCode ENDNot 100% guaranteed, but it should work.p.s. LTRIM is only for removing leading spaces, and it doesn't take 2 parameters. See Books Online.--Gail ShawSQL Server MVP |
 |
|
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2012-01-08 : 16:16:36
|
Not working :( quote: Originally posted by GilaMonster CASE WHEN ISNUMERIC(EmpCode) THEN CAST (CAST EmpCode AS int) AS varchar(7) ELSE EmpCode ENDNot 100% guaranteed, but it should work.p.s. LTRIM is only for removing leading spaces, and it doesn't take 2 parameters. See Books Online.--Gail ShawSQL Server MVP
THANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-01-08 : 16:52:49
|
| And 'Not Working' means what? I can't see your screen, I don't have your data, I can't read your mind. If you want us to help you, you need to put in a little bit of effort.--Gail ShawSQL Server MVP |
 |
|
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2012-01-08 : 17:03:33
|
| Ok.. consider the below scenarioCode----003 073 140 T3R I want result of SQL as Code----3 73 140 T3R THANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-01-08 : 17:05:56
|
| DECLARE @Table TABLE (EmpCode char(3),name varchar(20))INSERT INTO @TableSELECT '003','Raj' UNIONSELECT '048','Kumar' UNIONSELECT '073','selvam' UNIONSELECT '100','gopal' UNIONSELECT '140','gomathi' UNIONSELECT 'T3R','Nayaki' UNIONSELECT 'YEU','Yasodha' UNIONSELECT 'OPE','Easwari' UNIONSELECT '063','Rani'select stuff(empcode,1,patindex('%[^0]%',empcode) -1,''),empcode from @tableJimEveryday I learn something that somebody else already knew |
 |
|
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2012-01-08 : 17:23:30
|
| thanks jim , it works !THANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-01-09 : 01:41:50
|
| orselect cast(empcode as int) from @Table where empcode not like '%[^0-9]%'MadhivananFailing to plan is Planning to fail |
 |
|
|
|