?
Solved

Nesting Case statement in SQL

Posted on 2014-10-28
3
Medium Priority
?
120 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 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