Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Is there any better way to write the lines below in SQL Server 2005?

CASE
WHEN (ID IS NULL)
   THEN 'YES'
WHEN (ID IS NOT NULL)
   THEN 'NO'
END AS ID_Value,
share|improve this question

migrated from serverfault.com Jul 11 '12 at 2:49

This question came from our site for professional system and network administrators.

3  
Define "better". –  Wesley Jul 11 '12 at 2:20
    
I'm not familiar with MSSQL but if it has something like IF-function in MySQL, you can write such code: IF(ID IS NULL, 'YES', 'NO') AS ID_Value –  Kondybas Jul 11 '12 at 6:54
1  
SQL Server 2012 has IIF but the question is tagged 2005. –  Martin Smith Jul 11 '12 at 6:56
    
You should be able to accomplish this with the statement below. isnull(nullif(isnull(ID,'Y'),ID),'N') –  Jason Cumberland Jul 12 '12 at 16:24
    
@Jason Assuming ID is a string, of course. Otherwise this expression should be elongated a little more by a call to CAST or CONVERT. –  Andriy M Jul 12 '12 at 18:30
show 2 more comments

1 Answer 1

Did you try:

CASE WHEN (ID IS NULL) THEN 'YES' ELSE 'NO' END AS ID_Value,

I only have access to 2008 right now, but I'd hope that this syntax would still work in 2005 (seems like something that would be part of the original definition of CASE).

share|improve this answer
    
Yes - I did try CASE WHEN (ID IS NULL) THEN 'YES' ELSE 'NO' END AS ID_Value But I am looking for some other better approach something like IF(ID IS NULL, 'YES', 'NO') AS ID_Value in the Ms Sql, so that everything can be in a single line. Any suggestions please –  user10061 Jul 11 '12 at 17:28
1  
This answer is in a single line. If you want you can put the whole statement in a single line. –  ypercube Jul 12 '12 at 16:29
1  
@Tripz if you use the same login with both Server Fault and Database Administrators your accounts will be "associated". Go ahead and supply (or create a StackExchange) OpenID with each account, and you can retain ownership of the question, and earn more reps. –  jcolebrand Jul 18 '12 at 15:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.