sql date format yyyy-mm-dd code example

Example 1: sql date format yyyy-mm-dd

-- Oracle:TO_DATE(string, format)
SELECT TO_DATE('2012-06-05', 'YYYY-MM-DD') FROM dual;
SELECT TO_DATE('05/06/2012 13:25:12', 'DD/MM/YYYY HH24:MI:SS') FROM dual;
-- SQL Server: CONVERT(data_type, string, style). Cf source link for style codes.
SELECT CONVERT(DATETIME, '2012-06-05', 102);		-- Raises error if impossible
SELECT TRY_CONVERT(DATETIME, '2012-06-05', 102);	-- Returns Null if impossible
-- MySQL: STR_TO_DATE(string, format): 
SELECT STR_TO_DATE('2012-06-05','%Y-%m,%d');

Example 2: SQL query to convert DD/MM/YYYY to YYYY-MM-DD

SELECT CONVERT(varchar(10), CONVERT(date, '13/12/2016', 103), 120)

Example 3: js date format yyyy-mm-dd

yourDate.toISOString().split('T')[0]

Example 4: sql convert date to string yyyy-mm-dd

select CONVERT(char(10), GetDate(),126)
/* 2020-12-23 */

Example 5: sql server date format yyyy-mm-dd

convert(varchar, [column_date], 20) --(date or datetime) to yyyy-MM-dd HH:mm:ss
convert(varchar, [column_date], 23) --(date or datetime) to yyyy-MM-dd

--ref https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/