Solved

How to use SQL Server EE using CASE to evaluate date of datetime field

Posted on 2011-09-21
4
198 Views
Last Modified: 2012-05-12
Select
      CASE WHEN MyDateTime >= '20100101' AND <= '20110101' THEN 'Between 2010 01 01 AND 2011 01 01' END as Period
FROM TABLE
 
I realize it is something like:
trunc(MyDateTime) or LEFT(MyDateTime,8)
But not sure of the overall syntax..

0
Comment
Question by:Jimmy_inc
  • 2
4 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 36577146
CASE WHEN DATEPART(yyyy,MyDateTime) = 2010 THEN 'Between 2010 01 01 AND 2011 01 01' END as Period

0
 
LVL 18

Accepted Solution

by:
lludden earned 125 total points
ID: 36577157
If you don't include an else, it will put nulls in

CASE WHEN DATEPART(yyyy,MyDateTime) = 2010 THEN 'Between 2010 01 01 AND 2011 01 01' ELSE 'Another Period' END as Period

Or

CASE WHEN MyDateTime BETWEEN '2010-01-01' AND '2011-01-01' THEN 'Between 2010 01 01 AND 2011 01 01' ELSE 'Another Period' END AS Period

You can add as many WHEN's as you want.
0
 

Author Closing Comment

by:Jimmy_inc
ID: 36578335
Thanks
0
 
LVL 5

Expert Comment

by:AlokJain0412
ID: 36578410

See Following is the Case When Syntex

CASE expression
  WHEN value1 THEN result1
  WHEN value2 THEN result2
  ...
  WHEN valueN THEN resultN
 
  [
    ELSE elseResult
  ]
END
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now