SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 3 days before date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peace
Constraint Violating Yak Guru

283 Posts

Posted - 05/10/2012 :  22:33:37  Show Profile  Reply with Quote
How can I get the date for 3 days before today?

Example today's date 11 May.
I would like to get 8,9,10 May.

select ... from ...
where
CONVERT(date,goDate)=DATEADD(Day,-1, datediff(day, 0,getdate()) )

visakh16
Very Important crosS Applying yaK Herder

India
48012 Posts

Posted - 05/10/2012 :  22:35:37  Show Profile  Reply with Quote

SELECT *
FROM table
WHERE goDate>=DATEADD(Day,datediff(day, 0,getdate()),-3)
AND goDate< DATEADD(Day,datediff(day, 0,getdate()),0)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

peace
Constraint Violating Yak Guru

283 Posts

Posted - 05/10/2012 :  22:39:51  Show Profile  Reply with Quote
Oh got it thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48012 Posts

Posted - 05/10/2012 :  22:49:07  Show Profile  Reply with Quote
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22469 Posts

Posted - 05/14/2012 :  07:32:06  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
You can find more such examples here
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

peace
Constraint Violating Yak Guru

283 Posts

Posted - 05/15/2012 :  02:47:12  Show Profile  Reply with Quote
how can i get data for every 1st of every month, of the previous month?
Go to Top of Page

peace
Constraint Violating Yak Guru

283 Posts

Posted - 05/15/2012 :  05:07:43  Show Profile  Reply with Quote
I get this but it return for this month.
How to get for previous month?

select dateadd(month,datediff(month,0,getdate()),-0) as first_day_of_month
select dateadd(month,datediff(month,0,getdate())+1,-1) as last_day_of_month
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8529 Posts

Posted - 05/15/2012 :  05:17:19  Show Profile  Visit webfred's Homepage  Reply with Quote
Select DateAdd(Month,DateDiff(Month,0,getdate())-1,0) as firstday,
       DateAdd(dd,-1,DateAdd(Month,DateDiff(Month,0,getdate()),0)) as lastday



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48012 Posts

Posted - 05/15/2012 :  22:04:18  Show Profile  Reply with Quote
quote:
Originally posted by peace

I get this but it return for this month.
How to get for previous month?

select dateadd(month,datediff(month,0,getdate()),-0) as first_day_of_month
select dateadd(month,datediff(month,0,getdate())+1,-1) as last_day_of_month



the part in blue is one which calculates the month elapsed since start of base date (1900). the current way it returns month count till current month because of getdate. you can subtract 1,2,3 etc depending how many months before you want to traverse from current month

see this also to see similar date logics

http://visakhm.blogspot.com/2010/01/some-quick-tips-for-date-formating.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22469 Posts

Posted - 05/16/2012 :  04:21:43  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by peace

I get this but it return for this month.
How to get for previous month?

select dateadd(month,datediff(month,0,getdate()),-0) as first_day_of_month
select dateadd(month,datediff(month,0,getdate())+1,-1) as last_day_of_month


Have you seen the link I posted? Loot at the examples shown as last page

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000