﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Sunil Chandurkar  / Output of Query / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 23:13:17 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>I wanted a non of the above answer since the single quotes would not print out.</description><pubDate>Fri, 21 Oct 2011 18:40:08 GMT</pubDate><dc:creator>annjunk</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Thanks L' Eomot Inversé for pointing it out. I overlook this while putting the explanation.</description><pubDate>Fri, 14 Oct 2011 07:16:58 GMT</pubDate><dc:creator>Sunil Chandurkar</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Good Question.Thanks</description><pubDate>Thu, 13 Oct 2011 23:43:19 GMT</pubDate><dc:creator>malleswarareddy_m</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>unfortunately i got it wrong. But got something new:-)</description><pubDate>Thu, 13 Oct 2011 19:31:29 GMT</pubDate><dc:creator>jayant.m.1</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>[quote][b]L' Eomot Inversé (10/10/2011)[/b][hr]Good question.But the real answer of course is "it depends" on what the setting of ansi_nulls is; it's on by default in an SSMS query window, so the given answer is correct there; it is incorrect in a context where ansi_nulls is off, which is the default for connections where the application or the connection provider doesn't explicitly it in the connection dialog (SMSS does, SQL 2000 QA did, but some things don't).  Fortunately the ansi_nulls setting is an issue which will go away - the ability to change this setting it is a deprecated feature, there are important features which don't work if it is off (indexed views, indexes on computed columns, query notifications), and there will be a new version of SQL Server in which ansi_nulls is effectively always on and can't be switched off.[/quote]One more vote for Toms explanation here.</description><pubDate>Wed, 12 Oct 2011 09:43:06 GMT</pubDate><dc:creator>Carlton Leach</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>nice question , it depends on [b]ansi_nulls[/b] settingsStatement 1 - set ansi_nulls offSELECT CASE COL1 WHEN NULL THEN 'Is Null'  WHEN '0' THEN 'Is Zero'  WHEN '1' THEN 'Is One' WHEN '2' THEN 'Is Two' ENDFROM( SELECT NULL AS Col1 UNION SELECT '0' AS Col1 UNION SELECT '1' AS Col1 UNION SELECT '2' AS Col1) TMPstatement 2 -set ansi_nulls onSELECT CASE COL1 WHEN NULL THEN 'Is Null'  WHEN '0' THEN 'Is Zero'  WHEN '1' THEN 'Is One' WHEN '2' THEN 'Is Two' ENDFROM( SELECT NULL AS Col1 UNION SELECT '0' AS Col1 UNION SELECT '1' AS Col1 UNION SELECT '2' AS Col1) TMPRegards Deepak</description><pubDate>Tue, 11 Oct 2011 22:50:19 GMT</pubDate><dc:creator>deepak.a</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>[quote][b]jigsm_shah (10/11/2011)[/b][hr]As the correct output is Null,'Is Zero', 'Is One', 'Is Two'.I want to know why NULL is also the part of the output.The explanation says that:The statement CASE COL1 WHEN NULL THEN 'Is Null' doesn't evaluate to TRUTH value as NULL represents MISSING value.Hence it returns NULL.As the statement CASE COL1 WHEN NULL THEN 'Is Null' is not evaluated to TRUTH,then in that case it should not return any values as there is no else condition mentioned in the statement.Kindly help me to understand this.[/quote]The answer is in the else condition -- if there's no else, then the result is null.You can verify this by adding an additional value in the tmp subquery, say 10 or 42), you'll get nulls for those rows.</description><pubDate>Tue, 11 Oct 2011 14:19:22 GMT</pubDate><dc:creator>john.moreno</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>As the correct output is Null,'Is Zero', 'Is One', 'Is Two'.I want to know why NULL is also the part of the output.The explanation says that:The statement CASE COL1 WHEN NULL THEN 'Is Null' doesn't evaluate to TRUTH value as NULL represents MISSING value.Hence it returns NULL.As the statement CASE COL1 WHEN NULL THEN 'Is Null' is not evaluated to TRUTH,then in that case it should not return any values as there is no else condition mentioned in the statement.Kindly help me to understand this.</description><pubDate>Tue, 11 Oct 2011 12:55:12 GMT</pubDate><dc:creator>jerry209</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Simple question, thanks for the reminder.</description><pubDate>Tue, 11 Oct 2011 04:33:20 GMT</pubDate><dc:creator>spaghettidba</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Excellent question. Thanks!</description><pubDate>Mon, 10 Oct 2011 14:49:17 GMT</pubDate><dc:creator>Rob Schripsema</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Thanks for the question.  It was a good caffination check this morning. :-)</description><pubDate>Mon, 10 Oct 2011 14:15:43 GMT</pubDate><dc:creator>Evil Kraig F</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>I'd like to suggest a modification of the answer.  What is happening is that the "CASE COLUMN WHEN" syntax does an equity comparision and then uses the result of that comparision, which of course fails with NULL, while the "CASE WHEN x" syntax uses the result of x.  Which is why the given query returns null.So, something like:The CASE's statement has two alternative syntaxs, "CASE ColumnName WHEN Value" does an equity comparision between the column and the given value.  This fails with NULLS unless ANSI_NULLS is off.  The other syntax is "CASE WHEN EXPRESSION", which evalutes an expression (which may use any available columns) and uses the result of that expression to determine whether the condition has been met and the associated THEN used.</description><pubDate>Mon, 10 Oct 2011 11:00:17 GMT</pubDate><dc:creator>john.moreno</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>good question - tks</description><pubDate>Mon, 10 Oct 2011 10:35:29 GMT</pubDate><dc:creator>OzYbOi d(-_-)b</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Great question - unfortunately I got it wrong.I thought it was too simple so I over compensated and assumed that the code would have "set ansi_nulls off" for the session since it is effectively saying "where col1 = null" rather than "where col1 is null".</description><pubDate>Mon, 10 Oct 2011 10:30:37 GMT</pubDate><dc:creator>James_B</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Nice question about something that comes up regularly for me. Especially in writing queries for reporting.</description><pubDate>Mon, 10 Oct 2011 10:03:34 GMT</pubDate><dc:creator>KWymore</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Simple but neat -- thank you!</description><pubDate>Mon, 10 Oct 2011 09:13:50 GMT</pubDate><dc:creator>Revenant</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Bugger!  I hate it when I know the right answer but click on the wrong choice! :angry:</description><pubDate>Mon, 10 Oct 2011 08:45:07 GMT</pubDate><dc:creator>Aaron N. Cutshall</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Good question, luckily read the question twice before answering.</description><pubDate>Mon, 10 Oct 2011 08:19:11 GMT</pubDate><dc:creator>Sherwin Anderson</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Thanks for the question.</description><pubDate>Mon, 10 Oct 2011 08:19:04 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Or:[code="sql"]SELECT CASE when Col1 is null then 'Is Null'else case col1  WHEN '0' THEN 'Is Zero'  WHEN '1' THEN 'Is One' WHEN '2' THEN 'Is Two' ENDendFROM( SELECT NULL AS Col1 UNION SELECT '0' AS Col1 UNION SELECT '1' AS Col1 UNION SELECT '2' AS Col1) TMP[/code]</description><pubDate>Mon, 10 Oct 2011 07:51:06 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>[quote][b]Sean Lange (10/10/2011)[/b][hrThis works except you can't differentiate between null and empty strings because your isnull will now treat them both the same. In some cases this is ok but in others it isn't.[/quote]If it matters then you can do something likeCASE IsNull(COL1,'avaluethatwillnotappearinthedata') WHEN 'avaluethatwillnotappearinthedata' THEN 'Is Null' </description><pubDate>Mon, 10 Oct 2011 07:39:52 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>I got an error when I executed the code - oh well :hehe:</description><pubDate>Mon, 10 Oct 2011 07:36:32 GMT</pubDate><dc:creator>BarbW</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>[quote][b]sqlzealot-81 (10/10/2011)[/b][hr]I guess, you can achieve by the below way,[code="sql"]SELECT CASE IsNull(COL1,'') WHEN '' THEN 'Is Null'  WHEN '0' THEN 'Is Zero'  WHEN '1' THEN 'Is One' WHEN '2' THEN 'Is Two' ENDFROM( SELECT NULL AS Col1 UNION SELECT '0' AS Col1 UNION SELECT '1' AS Col1 UNION SELECT '2' AS Col1) TMP[/code][/quote]This works except you can't differentiate between null and empty strings because your isnull will now treat them both the same. In some cases this is ok but in others it isn't.</description><pubDate>Mon, 10 Oct 2011 07:34:01 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Nice question - thanks</description><pubDate>Mon, 10 Oct 2011 06:25:08 GMT</pubDate><dc:creator>Stuart Davies</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Nice one! I almost missed it, but my spidy sense told me to look at the question again. Thanks.</description><pubDate>Mon, 10 Oct 2011 06:17:04 GMT</pubDate><dc:creator>Thomas Abraham</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Great question.Thanks,Matt</description><pubDate>Mon, 10 Oct 2011 06:12:43 GMT</pubDate><dc:creator>Mattrick</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>[quote][b]stewartc-708166 (10/10/2011)[/b][hr]This is a good question, showing that care needs to be exercised when working with NULLs[quote][b]Ajay.Kedar (10/10/2011)[/b][hr]I tried and got wrong in first select case :-)But wanted to know thatHow can we chack null in this case then?[/quote]As shown in the explanation, the way the CASE is structured will need to be changed somewhat, i.e.[code="sql"]SELECT CASE WHEN COL1 IS NULL THEN 'Is Null'   WHEN COL1 = '0' THEN 'Is Zero'   WHEN COL1 = '1' THEN 'Is One'  WHEN COL1 = '2' THEN 'Is Two'  END FROM (  SELECT NULL AS Col1  UNION  SELECT '0' AS Col1  UNION  SELECT '1' AS Col1  UNION  SELECT '2' AS Col1 ) TMP[/code][/quote]This is the form that I always use nowadays, rarely are any of the select conditions that are simple value type check expressions.</description><pubDate>Mon, 10 Oct 2011 06:11:29 GMT</pubDate><dc:creator>mbova407</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>good question!!:-)</description><pubDate>Mon, 10 Oct 2011 05:41:17 GMT</pubDate><dc:creator>rfr.ferrari</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Good question, I had to read it carefully.</description><pubDate>Mon, 10 Oct 2011 05:26:41 GMT</pubDate><dc:creator>Britt Cluff</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>great :-)I am happy with this answer.Thankd stewartc-708166.</description><pubDate>Mon, 10 Oct 2011 03:20:25 GMT</pubDate><dc:creator>Ajay.Kedar</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>I've a feeling the behaviour was different in a previous version of SQLServer, as I think we were bitten by it when code that worked, stopped working. I don't have anything pre-2005 to test on though, so I may well be getting confused with something else :-)</description><pubDate>Mon, 10 Oct 2011 03:19:59 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>This is a good question, showing that care needs to be exercised when working with NULLs[quote][b]Ajay.Kedar (10/10/2011)[/b][hr]I tried and got wrong in first select case :-)But wanted to know thatHow can we chack null in this case then?[/quote]As shown in the explanation, the way the CASE is structured will need to be changed somewhat, i.e.[code="sql"]SELECT CASE WHEN COL1 IS NULL THEN 'Is Null'   WHEN COL1 = '0' THEN 'Is Zero'   WHEN COL1 = '1' THEN 'Is One'  WHEN COL1 = '2' THEN 'Is Two'  END FROM (  SELECT NULL AS Col1  UNION  SELECT '0' AS Col1  UNION  SELECT '1' AS Col1  UNION  SELECT '2' AS Col1 ) TMP[/code]</description><pubDate>Mon, 10 Oct 2011 03:15:53 GMT</pubDate><dc:creator>Stewart "Arturius" Campbell</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Good question.But the real answer of course is "it depends" on what the setting of ansi_nulls is; it's on by default in an SSMS query window, so the given answer is correct there; it is incorrect in a context where ansi_nulls is off, which is the default for connections where the application or the connection provider doesn't explicitly it in the connection dialog (SMSS does, SQL 2000 QA did, but some things don't).  Fortunately the ansi_nulls setting is an issue which will go away - the ability to change this setting it is a deprecated feature, there are important features which don't work if it is off (indexed views, indexes on computed columns, query notifications), and there will be a new version of SQL Server in which ansi_nulls is effectively always on and can't be switched off.</description><pubDate>Mon, 10 Oct 2011 02:16:34 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Nice question, thanks!</description><pubDate>Mon, 10 Oct 2011 02:06:57 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Thank you very much for your reply.Yes, considering this example, it will work fine.But what if I already have space in Col1.For that value too, it will show me 'Is null'.</description><pubDate>Mon, 10 Oct 2011 01:31:25 GMT</pubDate><dc:creator>Ajay.Kedar</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>I guess, you can achieve by the below way,[code="sql"]SELECT CASE IsNull(COL1,'') WHEN '' THEN 'Is Null'  WHEN '0' THEN 'Is Zero'  WHEN '1' THEN 'Is One' WHEN '2' THEN 'Is Two' ENDFROM( SELECT NULL AS Col1 UNION SELECT '0' AS Col1 UNION SELECT '1' AS Col1 UNION SELECT '2' AS Col1) TMP[/code]</description><pubDate>Mon, 10 Oct 2011 00:31:04 GMT</pubDate><dc:creator>sqlzealot-81</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>I tried and got wrong in first select case :-)But wanted to know thatHow can we chack null in this case then?</description><pubDate>Mon, 10 Oct 2011 00:14:27 GMT</pubDate><dc:creator>Ajay.Kedar</dc:creator></item><item><title>RE: Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Really  good question. unfortunately i got it wrong. But got something new.Thanks:-)</description><pubDate>Mon, 10 Oct 2011 00:03:28 GMT</pubDate><dc:creator>Danny Ocean</dc:creator></item><item><title>Output of Query</title><link>http://www.sqlservercentral.com/Forums/Topic1187712-1303-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/75073/"&gt;Output of Query&lt;/A&gt;[/B]</description><pubDate>Sun, 09 Oct 2011 21:54:05 GMT</pubDate><dc:creator>Sunil Chandurkar</dc:creator></item></channel></rss>