[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Date Diff in SQL

Posted on 2016-07-20
6
Medium Priority
?
109 Views
Last Modified: 2016-07-21
In access would add this to my criteria: > CDate("1/1/" & Year(Now())-1  to get anything greater than 1/1/YYYY.  I need to calculate YTD metrics as well as PY YTD metrics.  

YTD:  1/1/2016-present
PYTD: 1/1/2015-12/31/2015

How can I translate this to SQL?  I need to automate this.
0
Comment
Question by:gracie1972
  • 3
  • 2
6 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41721922
Year - CAST(CAST(Year(SomeDate) as char(4)) + '0101' as date)

What's the logic for PYTD?  That can wildly differ from place to place, so check out my article SQL Server Calendar Table which gives you code and a demo on how to pull this off to handle custom periods.
0
 

Author Comment

by:gracie1972
ID: 41721927
For Prior Year I have to include all the dates greater than 1/1/2015.  (Current year - 1).

In access this worked:  > CDate("1/1/" & Year(Now())-1
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41721934
datediff (year,1,getdate ())
That gives you the number of years from 1901 until this year

Now add that to "year zero" (1900)

dateadd (year, datediff (year,1,getdate ()) ,0)

Which will return a datetime value of:
Jan 1 last year
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:gracie1972
ID: 41721948
This only returned greater than 1/1/2016, I need for this query to run and by default always go back 1 year, YTD starting at the beginning of the year.  

When I write the where statement like this, I only get > 1/1/2016.
WHERE dbo.VW_Tableau_Sell_Out.[WEEK] > dateadd (year, datediff (year,1,getdate ()) ,0)

If today were 7/19/2017, then the query should return > 1/1/2016, we are trying to automate this. Access cannot work any longer the data pulls are too large.
This worked in Access: WHERE (dbo_VW_Tableau_Sell_Out.WEEK)>CDate("1/1/" & Year(Now())-1)
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41722164
oops, sorry

===
select  dateadd (year, datediff (year,0,getdate ())-1 ,0)

0 in the formula above represents 1900-01-01

get the datediff, in years, from that 0 date until getdate()
then deduct 1
then add that number of years to the zero date

e.g.  if getdate() is in the year 2016, the returned date would be 2015-01-01

=== e.g. === to get all data for the previous year
select *
from some_table
where datetime_col >= dateadd (year, datediff (year,0,getdate ())-1 ,0) -- 1st Jan prev year
and datetime_col <  dateadd (year, datediff (year,0,getdate ()) ,0) -- 1st Jan this year
0
 

Author Closing Comment

by:gracie1972
ID: 41723377
Thank you!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question