Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Select specific day for previous month

$
0
0

This is not really a question but more of a solution.  I have been searching for a select statement for X day of previous month.  There are many helpful solutions for first day and last day but I never found a solution for say the 20th day of the previous month.  Sure I could just use DATEPART but I needed it in datetime format to insert in an SSRS report for a parameter default value.  I wanted to share the solution I came up with and see if anyone has anything better.

In the following query I am selecting the first day of last month then adding 19 days so I can get the 20th day of the previous month.

Select DATEADD(day, 19,DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0))



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>