Solved

Nesting Case statement in SQL

Posted on 2014-10-28
3
113 Views
Last Modified: 2014-10-28
I am tring to write a case statement where if today's date is between the InitDate and Stop Date field value = 1.

Some of my records have a StopDate with a 'NULL' value and some have a date.

Here is my code:

CASE WHEN dbo.NurInterventionOccurrences.InitDateTime <= getdate()
                      AND dbo.NurInterventionOccurrences.StopDateTime IS NULL THEN 1 ELSE

CASE WHEN dbo.NurInterventionOccurrences.InitDateTime <= getdate()
                      AND dbo.NurInterventionOccurrences.StopDateTime > getdate() then 1 else

0 END AS Cnt


There is something wrong with the syntax.

Can someone help me out?

Thanks

glen
0
Comment
Question by:GPSPOW
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40409032
Give this a whirl (btw there was another END missing) ..
 -- Better to calculate it once here then 1000's of times in a query
Declare @dt datetime = GETDATE()  

SELECT blah, blah, blah, 
CASE 
   WHEN InitDateTime <= @dt AND StopDateTime IS NULL THEN 1 
   ELSE
      CASE 
         WHEN InitDateTime <= @dt THEN 1
         ELSE 0 END 
   END as Cnt

Open in new window

btw I have an article called SQL Server CASE Solutions, and in the middle there's a demo of nested CASE blocks.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40409115
I think the way you described it -- "if today's date is between the InitDate and Stop Date field value = 1" -- is clearer than your existing code.  Thus, I changed the code to match the description, while simplifying the code:

CASE WHEN getdate() >= dbo.NurInterventionOccurrences.InitDateTime AND
                      getdate() < ISNULL(dbo.NurInterventionOccurrences.StopDateTime, '20790601') then 1 else 0 END AS Cnt

Btw, GETDATE() will only be evaluated once in any given SELECT statement.
0
 

Author Closing Comment

by:GPSPOW
ID: 40409640
Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question