I need the date as ddmmyyyy
without any spacers.
How can I do this?
I can get yyyymmdd
using CONVERT(VARCHAR, [MyDateTime],
Try this:
SELECT CONVERT(DATE, STUFF(STUFF('01012020', 5, 0, '-'), 3, 0, '-'), 103);
It works with SQL Server 2016.
CONVERT
style 103 is dd/mm/yyyy. Then use the REPLACE
function to eliminate the slashes.
SELECT REPLACE(CONVERT(CHAR(10), [MyDateTime], 103), '/', '')
I've been doing it like this for years;
print convert(char,getdate(),103)
I found a way to do it without replacing the slashes
select CONVERT(VARCHAR(10), GETDATE(), 112)
This would return: "YYYYMMDD"
SELECT REPLACE(CONVERT(VARCHAR(10), THEDATE, 103), '/', '') AS [DDMMYYYY]
As seen here: http://www.sql-server-helper.com/tips/date-formats.aspx
select replace(convert(VARCHAR,getdate(),103),'/','')
select right(convert(VARCHAR,getdate(),112),2) +
substring(convert(VARCHAR,getdate(),112),5,2) +
left(convert(VARCHAR,getdate(),112),4)