Solved

How to compare the day of a sql date to a day range

Posted on 2014-11-04
4
106 Views
Last Modified: 2014-11-05
I want to know how I can compare the day of a sql date to a date range.

What I want to do is select CREATED_DATE_FW and compare its day >=1 and <=10

CREATED_DATE_FW date examples:

2014-01-05
2014-01-15
2014-01-03
"
"
"
2014-11-03
0
Comment
Question by:66chawger
[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 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40422451
Just the day part?   Yes, use DATEPART to get the day, then make your comparison..

SELECT blah, blah, blah
CASE WHEN DATEPART(day, CREATED_DATE_FW) BETWEEN 1 and 10 THEN 'yes'
   ELSE 'no' end as some_date_range
FROM your_table ...

Open in new window

Eyeballeth thy article on SQL Server CASE Solutions, specifically the couple of pages that begins with 'CASE can accommodate a range of values by using BETWEEN'
0
 
LVL 2

Expert Comment

by:Pratik Makwana
ID: 40423919
Select  Convert (date, GetDate()),day(GetDate()) Where day(GetDate()) Between 1 AND 10

Select CREATED_DATE_FW, day(CREATED_DATE_FW)  From tablename Where day(CREATED_DATE_FW) Between 1 AND 10
0
 

Author Closing Comment

by:66chawger
ID: 40424136
HA!  Perfect!   Why I was making this more complicated than it had to be I have no idea.   Never crossed my mind to use the DATEPART function in conjunction with simple BETWEEN logic... Thanks!
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40424144
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

617 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