HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How do I retrieve data in SQL from a previous date in the pass 12 months in a WHERE CLAUSE using the date and time functions?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
previousthepasssqlwheredatetimeretrievemonthsusing

Problem

How do I retrieve data in SQL from a previous date in the pass 12 months in a WHERE CLAUSE using the date and time functions?

--ServiceDates in past 12 months
WHERE YEAR(CONVERT(DATE, CLM_FROM_DT)) = 2016.

Solution

Both of these should work depending on what you need.

For any date range:

WHERE CLM_FROM_DT BETWEEN '20160331' AND '20170331'


If you are looking for 1 year back from current date:

WHERE CLM_FROM_DT>DATEADD(year,-1,GETDATE())


If you are looking for 1 year back from specific date (2017-3-31) as per your comment:

WHERE CLM_FROM_DT>DATEADD(year,-1, '20170331')

Code Snippets

WHERE CLM_FROM_DT BETWEEN '20160331' AND '20170331'
WHERE CLM_FROM_DT>DATEADD(year,-1,GETDATE())
WHERE CLM_FROM_DT>DATEADD(year,-1, '20170331')

Context

StackExchange Database Administrators Q#174449, answer score: 4

Revisions (0)

No revisions yet.