Format Date in Tsql - Sql Server

Jul 22, 2019 BY xtreme
SQL Server 2012 or above versions

    SELECT TOP 1 FORMAT (GETDATE(), 'd', 'en-us')		--US -MM/DD/YYYY
    SELECT TOP 1 FORMAT (GETDATE(), 'd', 'fr-FR')		--FR -DD/MM/YYYY	
    SELECT TOP 1 FORMAT (GETDATE(), 'd', 'ja-JP')		--JP -YYYY/MM/DD


    SELECT TOP 1 FORMAT (GETDATE(), 'D', 'en-us')		--US -Monday, December 31, 2018
    SELECT TOP 1 FORMAT (GETDATE(), 'D', 'fr-FR')		--FR -lundi 31 décembre 2018
    SELECT TOP 1 FORMAT (GETDATE(), 'D', 'ja-JP')		--JP -2018年12月31日
	
SQL Server 2000 or above versions

    --RESULT IN STRING/VARCHAR
    SELECT CONVERT(VARCHAR(10), GETDATE(), 111)	    -- YYYY/MM/DD
    SELECT FORMAT(GETDATE(), 'yyyy-MM-dd')		     -- FORMAT CUSTOM DATE


    --GET DATE PART OF DATE AND TIME
    SELECT CAST(GETDATE() as date)			          -- DATE ONLY
    SELECT CONVERT(date, GETDATE())


    --GET TIME PART OF DATE AND TIME
    SELECT CAST(GETDATE() as time)			          -- TIME ONLY
    SELECT CONVERT(time , GETDATE() , 114)

    SELECT CAST(CURRENT_TIMESTAMP AS DATE)
	

Related