Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql server convert string to date

select Cast( '2021-09-22' as date ) as castedDate;
select convert(date,'20210922') as convertedDate;
Comment

convert date to datetime sql

-- 2 ways 
SELECT CONVERT(datetime, '2021-01-01');
SELECT CAST('2021-01-01' AS datetime);
Comment

sql convert varchar to date

SELECT convert(datetime, '09/10/2019', 101);
Comment

How to convert DateTime to VarChar SQL

-- Create test case
DECLARE @myDateTime DATETIME
SET @myDateTime = '2008-05-03'
-- Convert string
SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10)
Comment

sql convert varchar to date

SELECT convert(datetime, '09/10/2019', 101);
Comment

SQL convert date format

SQL SERVER: 

DATE ONLY FORMATS
select convert(varchar, getdate(), 1);
	output: 12/30/06 (mm/dd/yy)
select convert(varchar, getdate(), 2);	
	output: 06.12.30 (yy.mm.dd)
select convert(varchar, getdate(), 3);	
	output: 30/12/06 (dd/mm/yy)
select convert(varchar, getdate(), 4);	
	output: 30.12.06 (dd.mm.yy)	
select convert(varchar, getdate(), 5);
	output: 30-12-06 (dd-mm-yy)
select convert(varchar, getdate(), 6);	
	output: 30 Dec 06 (dd-Mon-yy)
select convert(varchar, getdate(), 7);	
	output: Dec 30, 06 (Mon dd, yy)
select convert(varchar, getdate(), 10);	
	output: 12-30-06 (mm-dd-yy)
select convert(varchar, getdate(), 11);
	output: 06/12/30 (yy/mm/dd)
select convert(varchar, getdate(), 12);	
	output: 061230 (yymmdd)
select convert(varchar, getdate(), 23);
	output: 2006-12-30 (yyyy-mm-dd)
select convert(varchar, getdate(), 101);
	output: 12/30/2006 (mm/dd/yyyy)
select convert(varchar, getdate(), 102);
	output: 2006.12.30 (yyyy.mm.dd)
select convert(varchar, getdate(), 103);
	output: 30/12/2006 (dd/mm/yyyy)
select convert(varchar, getdate(), 104);
	output: 30.12.2006 (dd.mm.yyyy)
select convert(varchar, getdate(), 105);
	output: 30-12-2006 (dd-mm-yyyy)
select convert(varchar, getdate(), 106);
	output: 30 Dec 2006 (dd Mon yyyy)
select convert(varchar, getdate(), 107);
	output: Dec 30, 2006 (Mon dd, yyyy)
select convert(varchar, getdate(), 110);	
	output: 12-30-2006 (mm-dd-yyyy)
select convert(varchar, getdate(), 111);	
	output: 2006/12/30 (yyyy/mm/dd)
select convert(varchar, getdate(), 112);
	output : 20061230 (yyyymmdd)
 	
TIME ONLY FORMATS
select convert(varchar, getdate(), 8);
	output: 00:38:54 (hh:mm:ss)
select convert(varchar, getdate(), 14);	
	output: 00:38:54:840 (hh:mm:ss:nnn)
select convert(varchar, getdate(), 24);	
	output: 00:38:54 (hh:mm:ss)
select convert(varchar, getdate(), 108);
	output: 00:38:54 (hh:mm:ss)
select convert(varchar, getdate(), 114);
	output: 00:38:54:840 (hh:mm:ss:nnn)
 	
DATE & TIME FORMATS
select convert(varchar, getdate(), 0);	
	output: Dec 30 2006 12:38AM (Mon dd yyyy hh:mm AM/PM)
select convert(varchar, getdate(), 9);
	output: Dec 30 2006 12:38:54:840AM (Mon dd yyyy hh:mm:ss:nnn AM/PM)
select convert(varchar, getdate(), 13);
	output: 30 Dec 2006 00:38:54:840AM (dd Mon yyyy hh:mm:ss:nnn AM/PM)
select convert(varchar, getdate(), 20);	
	output: 2006-12-30 00:38:54 (yyyy-mm-dd hh:mm:ss)
select convert(varchar, getdate(), 21);	
	output: 2006-12-30 00:38:54.840 (yyyy-mm-dd hh:mm:ss:nnn)
select convert(varchar, getdate(), 22);	
	output: 12/30/06 12:38:54 AM (mm/dd/yy hh:mm:ss AM/PM)
select convert(varchar, getdate(), 25);
	output: 2006-12-30 00:38:54.840 (yyyy-mm-dd hh:mm:ss:nnn)
select convert(varchar, getdate(), 100);
	output: Dec 30 2006 12:38AM (Mon dd yyyy hh:mm AM/PM)
select convert(varchar, getdate(), 109);	
	output: Dec 30 2006 12:38:54:840AM (Mon dd yyyy hh:mm:ss:nnn AM/PM)
select convert(varchar, getdate(), 113);
	output: 30 Dec 2006 00:38:54:840 (dd Mon yyyy hh:mm:ss:nnn)
select convert(varchar, getdate(), 120);
	output: 2006-12-30 00:38:54 (yyyy-mm-dd hh:mm:ss)
select convert(varchar, getdate(), 121);
	output: 2006-12-30 00:38:54.840 (yyyy-mm-dd hh:mm:ss:nnn)
select convert(varchar, getdate(), 126);
	output: 2006-12-30T00:38:54.840 (yyyy-mm-dd T hh:mm:ss:nnn)
select convert(varchar, getdate(), 127);	
	output: 2006-12-30T00:38:54.840 (yyyy-mm-dd T hh:mm:ss:nnn	)
 	
ISLAMIC CALENDAR DATES
select convert(nvarchar, getdate(), 130);
select convert(nvarchar, getdate(), 131);	
	output: 10/12/1427 12:38:54:840AM (dd mmm yyyy hh:mi:ss:nnn AM/PM)

select replace(convert(varchar, getdate(),101),'/','');
	output: 12302006 (mmddyyyy)
select replace(convert(varchar, getdate(),101),'/','') + replace(convert(varchar, getdate(),108),':','');	
	output: 12302006004426 (mmddyyyyhhmmss)
Comment

cast datetime to date in sql

SELECT CONVERT(date, getdate());
Comment

sql convert datetime

declare @Existingdate datetime
Set @Existingdate=GETDATE()
Select CONVERT(varchar,@Existingdate,103) as [DD/MM/YYYY]
Comment

sql cast date to 103

cast date to 103
Comment

convert varchar to time sql

-- convert 24 hr text to AM/PM text time
CONVERT(VARCHAR(15),FORMAT(CAST('16:00' as datetime), 'hh:mm tt'))
-- returns 04:00 PM
Comment

PREVIOUS NEXT
Code Example
Sql :: update with select postgresql 
Sql :: how to check table lock 
Sql :: how to identify locked tables in sql server 
Sql :: function in plsql 
Sql :: eliminate zero from integer mysql 
Sql :: sql check duplicate value in column 
Sql :: postgres regular expression replace 
Sql :: oracle leftmost characters 
Sql :: in mysql workbench contnent not feching 
Sql :: t-sql create trigger 
Sql :: check if a column is a primary key in sql server 
Sql :: python sqlite data delete 
Sql :: sql count null values in all columns 
Sql :: how to find third highest salary in sql 
Sql :: python sqlite3 update 
Sql :: oracle drop sequence 
Sql :: mysql group by date 
Sql :: launch sql script from docker in mysql 
Sql :: convert multiple columns to rows in sql server 
Sql :: postgresql delete multiple rows 
Sql :: mysql compare timestamp in minutes 
Sql :: sql character index 
Sql :: insert snowflake 
Sql :: mysql 8 geo to json 
Sql :: update sqlite 
Sql :: mysql create table from select statement 
Sql :: nosql vs sql 
Sql :: sql server list database 
Sql :: odd record sql query 
Sql :: mysql count rows returned 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =