Solved

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

Posted on 2014-11-04
4
101 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
  • 2
4 Comments
 
LVL 65

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 65

Expert Comment

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

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

821 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