| Author |
Topic |
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2012-01-16 : 11:33:30
|
| I want to make a CASE WHEN to this statement: CONVERT(VARCHAR, DTR.DATE_ORDER, 110) + ' ' + SUBSTRING(CONVERT(VARCHAR, AM.ADMIN_TIME, 100), 13, 7) ADMIN_TIME because I think that AM.ADMIN_Time is null in some cases and this is causing the app to fail.When I add this ,CASE WHEN AM.ADMIN_TIME is not null THENwith a ELSE '' at the end,it does not compile. HOw in the above Convert statement can I provide for nulls and not process them? |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-01-16 : 11:46:59
|
| It might help if you post DDL, DML and expected outout per this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxBut, on top of that, what do you mean by "not process them?" Do you mean ommit the rows with NULL AM.ADMIN_TIME from your result set or setting the result of the CASE expression to an empty sting? If the former, then i'd suggest using a WHERE clasue: WHERE AM.ADMIN_TIME IS NOT NULL. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-16 : 11:48:17
|
just useCOALESCE(CONVERT(VARCHAR, DTR.DATE_ORDER, 110) + ' ','') + COALESCE(SUBSTRING(CONVERT(VARCHAR, AM.ADMIN_TIME, 100), 13, 7),'') ADMIN_TIME ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2012-01-16 : 14:01:54
|
Yes I would only omit the date/time data if its not there or if it's bad (error) data. WHere do i place this WHERE CLAUSE?quote: Originally posted by Lamprey It might help if you post DDL, DML and expected outout per this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxBut, on top of that, what do you mean by "not process them?" Do you mean ommit the rows with NULL AM.ADMIN_TIME from your result set or setting the result of the CASE expression to an empty sting? If the former, then i'd suggest using a WHERE clasue: WHERE AM.ADMIN_TIME IS NOT NULL.
|
 |
|
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-16 : 14:10:33
|
quote: Originally posted by AdamWest you mean give a legnth for ADMIN TIME?quote: Originally posted by visakh16 also make sure you specify a length whenever you cast to varchar else the below issue can occurhttp://visakhm.blogspot.com/2010/02/importance-of-specifying-length-in.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
not only thateverywhere you do cast to varchar,numeric etc specify length,precision etcgo through link for reasons------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2012-01-16 : 14:34:53
|
quote: Originally posted by visakh16 everywhere you do cast to varchar,numeric etc specify length,precision etc
Absolutely! Worries me a lot when I see folk relying on the "default-length" for "varchar" |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-01-16 : 14:58:07
|
quote: Originally posted by AdamWest Yes I would only omit the date/time data if its not there or if it's bad (error) data. WHere do i place this WHERE CLAUSE?quote: Originally posted by Lamprey It might help if you post DDL, DML and expected outout per this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxBut, on top of that, what do you mean by "not process them?" Do you mean ommit the rows with NULL AM.ADMIN_TIME from your result set or setting the result of the CASE expression to an empty sting? If the former, then i'd suggest using a WHERE clasue: WHERE AM.ADMIN_TIME IS NOT NULL.
If you don't know where to put a where clause, then I suggest you get a book or read some tutorials on SQL. I can say that it all starts with a SELECT statement. Here is a link to get you started:http://msdn.microsoft.com/en-us/library/ms189499.aspx |
 |
|
|
|