Solved

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

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

705 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

20 Experts available now in Live!

Get 1:1 Help Now