SQLTeam.com | Weblogs | Forums

SQL Need 1 month Prior of Data


#1

I need to use my field called clm_dout to pull the prior months data. I tried using this below, however it takes a long time to run. Is there something else I can use instead? Any help would be great, thanks!

clm_dout BETWEEN CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + CAST(MONTH(DATEADD(month, -1, GETDATE())) AS VARCHAR(2)) + '-20 00:00:00'
AND CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + CAST(MONTH(GETDATE()) AS VARCHAR(2)) + '-20 00:00:00'


#2
 clm_dout BETWEEN dateadd(month,-1,dateadd(day,19,dateadd(month,datediff(month,0,getdate()),0)))
 AND  dateadd(day,19,dateadd(month,datediff(month,0,getdate()),0))

#3

Hi wsilage,

Here you can find my solution:

...
WHERE CONVERT(VARCHAR(6), clm_dout, 112) = CONVERT(VARCHAR(6), DATEADD(MONTH, -1, GETDATE()), 112)

Regards,
gigawatt38
MCSA: SQL 2016 Database Development
MCSE: Data Management and Analytics

Tested by: http://rextester.com/l/sql_server_online_compiler


#4

Thanks to everyone all your solutions have worked for me and is running faster.