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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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
1  
@Bappy1988 Why would it be better than Jay's answer? – dezso Jul 18 '12 at 15:03

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
4  
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

protected by Community Feb 1 at 22:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?