Solved

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

Posted on 2014-11-04
4
97 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

774 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