Forum Discussion

A819A1L's avatar
A819A1L
Brass Contributor
May 24, 2023

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,

  • 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))
  • A819A1L's avatar
    A819A1L
    Brass Contributor

    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 🙂 

    • olafhelper's avatar
      olafhelper
      Bronze Contributor

      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))

Resources