Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-11-04
4
Medium Priority
?
115 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 66

Accepted Solution

by:
Jim Horn earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

772 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