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
 Sql Server 2008/R2 date operations
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mjimenezh
Yak Posting Veteran

Mexico
61 Posts

Posted - 07/24/2013 :  19:52:08  Show Profile  Reply with Quote
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

Edited by - mjimenezh on 07/24/2013 19:53:30

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/24/2013 :  19:58:49  Show Profile  Reply with Quote
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)?

Edited by - MuMu88 on 07/24/2013 19:59:19
Go to Top of Page

mjimenezh
Yak Posting Veteran

Mexico
61 Posts

Posted - 07/24/2013 :  20:01:29  Show Profile  Reply with Quote
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

547 Posts

Posted - 07/24/2013 :  20:12:33  Show Profile  Reply with Quote
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

Mexico
61 Posts

Posted - 07/24/2013 :  20:26:42  Show Profile  Reply with Quote
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

547 Posts

Posted - 07/24/2013 :  20:57:36  Show Profile  Reply with Quote
Try this:


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




Edited by - MuMu88 on 07/24/2013 22:03:32
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.06 seconds. Powered By: Snitz Forums 2000