Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2008-01-03 : 17:09:56
|
[code]I have written a query which zero's are truncating in the resultsselect testid =(Case When testid = 1234 then '001' When Testid = 1324 then '002' When Testid = 1224 then '003'else testid end)from tableI got the below resultsTestid------123Where as i need the results Testid-----001002003Thanks for your help in advance[/code] |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-01-03 : 17:24:01
|
That is expected as TestID is an Integer, correct? Try this: select testid =(Case When testid = 1234 then '001' When Testid = 1324 then '002' When Testid = 1224 then '003'else CAST(testid AS VARCHAR(8)) end)from table |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-01-03 : 17:33:50
|
[code]select testid = case when testid = 1234 then '001' when Testid = 1324 then '002' when Testid = 1224 then '003' else right('000'+convert(varchar(20),testid),3) endfrom table[/code]CODO ERGO SUM |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-04 : 08:29:40
|
quote: Originally posted by sqldba2k6
I have written a query which zero's are truncating in the resultsselect testid =(Case When testid = 1234 then '001' When Testid = 1324 then '002' When Testid = 1224 then '003'else testid end)from tableI got the below resultsTestid------123Where as i need the results Testid-----001002003Thanks for your help in advance
If testid's value is 25678 then is this your expected result?Testid----- 001 002 00325678MadhivananFailing to plan is Planning to fail |
 |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2008-01-04 : 10:24:50
|
[code]Thanks it works..I want to get one more results with my query..Can i write in better way....select testid =(Case When testid = 1234 then '001' When testid = 1234 then testid1='02' When Testid = 1324 then '002' When testid = 1324 then testid1='03' When Testid = 1224 then '003' When Testid = 1224 then '06'else testid end)from tableI want the ouput like below.Testid Testid1------ -------001 02002 03 003 06[/code] |
 |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2008-01-04 : 10:24:51
|
[code]Thanks it works..I want to get one more results with my query..Can i write in better way....select testid =(Case When testid = 1234 then '001' When testid = 1234 then testid1='02' When Testid = 1324 then '002' When testid = 1324 then testid1='03' When Testid = 1224 then '003' When Testid = 1224 then '06'else testid end)from tableI want the ouput like below.Testid Testid1------ -------001 02002 03 003 06[/code] |
 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-01-04 : 12:06:12
|
you need two case expressions to do that. one for the Testid column, and another one for the Testid1 column. elsasoft.org |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-07 : 00:55:35
|
quote: Originally posted by sqldba2k6
Thanks it works..I want to get one more results with my query..Can i write in better way....select testid =(Case When testid = 1234 then '001' When testid = 1234 then testid1='02' When Testid = 1324 then '002' When testid = 1324 then testid1='03' When Testid = 1224 then '003' When Testid = 1224 then '06'else testid end)from tableI want the ouput like below.Testid Testid1------ -------001 02002 03 003 06
Did you read my previous post?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|