Solved

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

Posted on 2011-09-21
4
202 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
[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
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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