SOLVED

# Convert DATETIME to YYYYMMDD

Brass Contributor

# Convert DATETIME to YYYYMMDD

Hello,

I'm struggling to work out how to achieve today's date in numbers using GETDATE().

The result I need is 20230524. So today's date in 8 digits as int starting with year: YYYYMMDD

Thanks,

5 Replies

# Re: Convert DATETIME to YYYYMMDD

I've got it:

``````SELECT CAST(
CONCAT(
RIGHT('0' + RTRIM(YEAR(GETDATE())), 4),
RIGHT('0' + RTRIM(MONTH(GETDATE())), 2),
RIGHT('0' + RTRIM(DAY(GETDATE())), 2)
) AS INT )``````

If anyone knows of a better way, feel free to educate me :)

best response confirmed by A819A1L (Brass Contributor)
Solution

# Re: Convert DATETIME to YYYYMMDD

If anyone knows of a better way, feel free to educate me :)

@A819A1L , there is one:

``````-- Date format 112 = ISO = YYYYMMDD
SELECT CONVERT(int, CONVERT(char(10), GETDATE(), 112))``````

# Re: Convert DATETIME to YYYYMMDD

@olafhelper amazing thanks!

# Re: Convert DATETIME to YYYYMMDD

Have you looked up and studied the CONVERT() function that OlafHelper posted to learn something new?

# Re: Convert DATETIME to YYYYMMDD

Yes, I did. I have used convert many times in the past, I just couldn't find the code for the format I needed at the time I needed it. So in my haste, I posted here.

As always, I appreciate the information supplied and effort volunteers put in to help me out :thumbs_up:

1 best response

Accepted Solutions
best response confirmed by A819A1L (Brass Contributor)
Solution

# Re: Convert DATETIME to YYYYMMDD

If anyone knows of a better way, feel free to educate me :)

@A819A1L , there is one:

``````-- Date format 112 = ISO = YYYYMMDD
SELECT CONVERT(int, CONVERT(char(10), GETDATE(), 112))``````