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

Change date parameters to find data from previous month

$
0
0

hI,

I wrote the below SQL query:

DECLARE @MINDATE DATETIME
DECLARE @MAXDATE DATETIME
SET @MINDATE = '2013-08-01'
SET @MAXDATE = '2013-08-31'
SELECT	A.SALESID,
		A.SHIPPINGDATECONFIRMED, 
		B.CREATEDDATE,
		A.SALESSTATUS
FROM SALESTABLE A
INNER JOIN (SELECT 
				SALESID,
				CREATEDDATE,
				ROW_NUMBER() OVER (PARTITION BY SALESID ORDER BY RECID DESC ) AS RN
			FROM dbo.CUSTPACKINGSLIPTRANS) B ON A.SALESID = B.SALESID
WHERE A.SALESSTATUS IN ('2', '3')
AND B.RN = 1
AND B.CREATEDDATE BETWEEN @MINDATE AND @MAXDATE
AND A.SHIPPINGDATECONFIRMED = '1900-01-01'

I'd like to change it so that rather than using a specific date for the parameters I would like to get all records where the CREATEDDATE is in the previous month. Is there any easy way of doing this?

Also, if theres a more efficient way of writing this I'd bee keen to hear it.

Cheers

Paul


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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