Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Relative Dates

Status
Not open for further replies.

robertfah

Programmer
Joined
Mar 20, 2006
Messages
380
Location
US
I'm writing a program that creates simple SQL based on Relative dates instead of real dates. So a user can pick an operator (<, >, =) and then choose a relative date:

Yesterday
Today
Tomorrow
ThisWeek
LastWeek
ThisMonth
LastMonth
ThisYear
LastYear

and the code would spit out something like this:

Code:
SELECT i.DateCreated
FROM Items i 
WHERE 1=1
AND i.DateCreated < getdate()

Does anyone know I can calculate the relative dates? for instance, if they pick Tomorrow, then how would I use getdate() to make it tomorrow? and so on for the rest of the relative dates?

Thanks in advance!
 
I figured out a few of them, but does anyone know how I can get the last 3 (the ones with the ???)?

Code:
SELECT 'Today', getdate()
SELECT 'Tomorrow', DATEADD(day, 1, getdate())
SELECT 'Yesterday', DATEADD(day, -1, getdate())
SELECT 'LastWeek', DATEADD(day, -7, getdate())
SELECT 'LastMonth', DATEADD(Month, -1, getdate())
SELECT 'LastYear', DATEADD(Year, -1, getdate())
SELECT 'ThisWeek', '???'
SELECT 'ThisMonth', '???'
SELECT 'ThisYear', '???'
 
Adding a Date table to your database comes in really handy. For every date, you will have columns to store the year, month, quarter, week, etc. Anyways, if you don't want to go that route, you can use something like the following:

Code:
--Start of Week
SELECT DATEADD(day, DATEPART(dw, GETDATE()) * -1, GETDATE())

--End of Week
SELECT DATEADD(day, 7 - DATEPART(dw, GETDATE()) , GETDATE())

--Start of Month
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(4), YEAR(GETDATE())) + RIGHT('0' + CONVERT(VARCHAR(2), MONTH(GETDATE())),2) + '01')

--End of Month
SELECT DATEADD(day, -1, DATEADD(month, 1, CONVERT(DATETIME, CONVERT(VARCHAR(4), YEAR(GETDATE())) + RIGHT('0' + CONVERT(VARCHAR(2), MONTH(GETDATE())),2) + '01')))

--Start of Year
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(4), YEAR(GETDATE())) + '0101')

--End of Year
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(4), YEAR(GETDATE())) + '1231')

Be sure you understand how DATEFIRST works when working with days of the week. Also, depending on your situation, you might need to strip off the time portions of your date when using GETDATE().
 
Or just to be a killjoy, could you not give them 2 parameters - startdate and enddate and then use those to do a

Code:
where datecreated >= @startdate 
and datecreated <= @enddate

?

Cheers,

M.
 
Hi,

You can use the following:

Code:
// This week. Note depending on what set for @@datefirst this could be Monday or Sunday
select DATEADD(dd, 1 - DATEPART(dw, getdate()), getdate())

// This Month
select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

// This Year
select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

Ryan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top