Solved

Nesting Case statement in SQL

Posted on 2014-10-28
3
118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 66

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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