Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql Server 2008/R2 date operations

Author  Topic 

mjimenezh
Yak Posting Veteran

81 Posts

Posted - 2013-07-24 : 19:52:08
Hi everyone, how is the perfect way to create this line of code I need the records between today and the date -1,1,1:

This is my code :

select * between getdate() and getdate(year(getdate)-1,1,1)  


what is wrong and what is meaning the -1,1,1 part?

Thanks

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-24 : 19:58:49
what do you mean by "the date -1, 1,1"?
Is it Aug,25, 2012 if today is July, 24, 2013?
(Year-1, Month+1, Day+1)?
Go to Top of Page

mjimenezh
Yak Posting Veteran

81 Posts

Posted - 2013-07-24 : 20:01:29
I was talking about this line of code, I was not sure getdate(year(getdate)-1,1,1) about the function of the -1,1,1 , I need to define the select * between today and that date.
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-24 : 20:12:33
As far as I know, getdate() does not take any arguments, it returns todays date and time.
What is the date you are trying to get by performing this operation getdate(year(getdate)-1,1,1)
Go to Top of Page

mjimenezh
Yak Posting Veteran

81 Posts

Posted - 2013-07-24 : 20:26:42
This view must have to compare the current period with the same period but from the last year
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-24 : 20:57:36
Try this:
[CODE]

select * FROM YOUR_TABLE_NAME WHERE DATE_COUMN_NAME
BETWEEN dateadd(YY, -1, getdate()) AND getdate(); -- Gives you data for the past year

select * FROM YOUR_TABLE_NAME WHERE DATE_COUMN_NAME
BETWEEN dateadd(month, -6, getdate()) AND getdate(); -- Gives you data for the past 6 months

select * FROM YOUR_TABLE_NAME WHERE DATE_COUMN_NAME
BETWEEN dateadd(day, -30, getdate()) AND getdate(); -- Gives you data for the past 30 days


[/CODE]
Go to Top of Page
   

- Advertisement -