spirose
asked on
SQL Server - Query syntax for yesterday's date
How do I Parse yesterday's date so that it shows up as 20101107 (yyyymmdd)
(Note that if the day or month is a single digit number, it should be preceded by a zero '0')
(Note that if the day or month is a single digit number, it should be preceded by a zero '0')
SELECT DATEADD(day, -1, getdate())
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT CONVERT(VARCHAR(8), GETDATE()-1, 112) AS [YYYYMMDD]
Try:
SELECT CONVERT(VARCHAR(8), DATEADD(day, -1, GETDATE()), 112) AS [YYYYMMDD]
(found here: http://www.sql-server-helper.com/tips/date-formats.aspx)
SELECT CONVERT(VARCHAR(8), DATEADD(day, -1, GETDATE()), 112) AS [YYYYMMDD]
(found here: http://www.sql-server-helper.com/tips/date-formats.aspx)
Sorry, ignoreme - you need the DATEADD as per vdr1620. I'd still go varchar(8) though...
Sorry to mess this thread up totally, but actually my 'correction' was wrong: originally-posted query works fine. I would imagine GETDATE()-1 is very marginally quicker than using the DATEADD; main advantage in my view though is that it's just easier to read.
SELECT CONVERT(VARCHAR(8), GETDATE()-1, 112) AS [YYYYMMDD]