May 24 2023 02:29 AM - edited May 24 2023 03:19 AM
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,
May 24 2023 03:29 AM
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 :)
May 24 2023 10:21 PM
SolutionIf 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))
May 28 2023 07:52 AM
May 28 2023 08:00 AM
Hi @Jeff_Moden
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:
May 24 2023 10:21 PM
SolutionIf 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))