Web Analytics Made Easy - Statcounter

Convert () function in SQL Server

Convert () function is used to convert an expression from one data type to another data typeSyntax for CONVERT function
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

Convert functions is usually used with date/time data type to convert a date in different formats. Below examples will show the different formats in which we can display the date/time data type using Convert function. In the below examples we use getdate() to get the current date and time of the database server.

select convert(varchar(40), getdate(), 0)

Result:-
Jan 25 2015  9:36PM

select convert(varchar(40), getdate(), 101)

Result:-
01/25/2015

select convert(varchar(40), getdate(), 102)

Result:-
2015.01.25

select convert(varchar(40), getdate(), 103)

Result:-
25/01/2015

select convert(varchar(40), getdate(), 104)

Result:-
25.01.2015

select convert(varchar(40), getdate(), 105)

Result:-
25-01-2015

select convert(varchar(40), getdate(), 106)

Result:-
25 Jan 2015

select convert(varchar(40), getdate(), 107)

Result:-
Jan 25, 2015

select convert(varchar(40), getdate(), 108)

Result:-
21:39:21

select convert(varchar(40), getdate(), 109)

Result:-
Jan 25 2015 10:19:15:583PM


select convert(varchar(40), getdate(), 110)

Result:-
01-25-2015


select convert(varchar(40), getdate(), 111)

Result:-
2015/01/25


select convert(varchar(40), getdate(), 112)

Result:-
20150125


select convert(varchar(40), getdate(), 113)

Result:-
25 Jan 2015 23:22:49:453


select convert(varchar(40), getdate(), 114)

Result:-
23:23:09:970


select convert(varchar(40), getdate(), 120)

Result:-
2015-01-25 23:23:37


select convert(varchar(40), getdate(), 121)

Result:-
2015-01-25 23:24:07.577


select convert(varchar(40), getdate(), 126)

Result:-
2015-01-25T23:24:29.860


select convert(varchar(40), getdate(), 127)

Result:-
2015-01-25T23:24:55.037


select convert(varchar(40), getdate(), 131)

Result:-
5/04/1436 11:25:23:483PM

From the above examples we can see that how we can use the Convert () function to display date time in different formats.


Discover more from Technology with Vivek Johari

Subscribe to get the latest posts sent to your email.

By vivekjohari

I am currently working as a Senior Database Professional and have around 18 years of experience in database. Degree:- Master Degree in Computer(MCA) Certification course in Data Science & Machine Learning from Indian Institute of Technology (IIT), Delhi Work experience:- Designing of the database. Database Optimization. Writing Complex Stored Procedures,Functions,Triggers etc. Designing and developing SSIS & DTS packages. Designing SQL Reports using SSRS. Database Server Maintenance. Certification:- MCTS: DA-100: Analysing Data with Microsoft Power BI MCTS: DP-300: Administering Relational Databases on Microsoft Azure Microsoft certified Sql DBA in Sql server 2008 (MCTS). Microsoft certified BI professional in Sql server 2008 (MCTS). Oracle certified profession DBA in ORACLE 10g (OCP) certified profession DBA in ORACLE 9i (OCP) My other publication Technical Blog:- Technologies with Vivek Johari Guest Author and Blogger at sqlservercentral.com

31 thought on “SQL Server – Convert() function”
  1. … [Trackback]

    […] Here you can find 90024 additional Info on that Topic: techmixing.com/2015/01/sql-server-convert-function.html […]

Leave a Reply

Discover more from Technology with Vivek Johari

Subscribe now to keep reading and get access to the full archive.

Continue reading