[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Date Diff in SQL

Posted on 2016-07-20
6
Medium Priority
?
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

649 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