Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to write the query only returns records where the expiry date is 5 days from current date.
Posted

Try this:
SQL
DECLARE @ToDate DATETIME = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
DECLARE @FromDate DATETIME = DATEADD(dd, -5, @ToDate)

SELECT <Field_list>
FROM <TableName>
WHERE DateTimeField BETWEEN @FromDate AND @ToDate


Note: change the code to your needs

See also:
DATEADD()[^]
DATEDIFF()[^]
 
Share this answer
 
v2
so, what is your problem ? where are you getting stuck ?

you might want to look at DATEADD() and GETDATE() functions for helping you with the date math
 
Share this answer
 
Use the dateadd function in your where clause

see http://msdn.microsoft.com/en-us/library/ms186819.aspx[^]

i.e where expirydate >= dateadd("d",5,getdate())

Getdate() returns the current date. You will need to be careful of the time part of your query depending on what you are doing.
 
Share this answer
 
Comments
aarif moh shaikh 20-Oct-14 0:11am    
yes, write..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900