Solved

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

Posted on 2011-09-21
4
197 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0
 
LVL 5

Expert Comment

by:AlokJain0412
Comment Utility

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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.​
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

8 Experts available now in Live!

Get 1:1 Help Now