SQL

How To Use Date Formats In SQL

In this article, we will learn how to use the Date Formats in SQL.

SQL Server has many built-in Date Formats.

GETDATE() is a date function used to return the current database system date and time.

FORMAT() is a string function used to format a value with the specified format.

 

Format SQL Statement
MM/dd/yyyy
declare @date datetime = '2019-01-01'
SELECT FORMAT(@date, 'MM/dd/yyyy');

Output: 01/01/2019

M/dd/yyyy
declare @date datetime = '2019-01-01'
SELECT FORMAT(@date, 'M/dd/yyyy');

Output: 1/01/2019

dddd, MMMM dd, yyyy
SELECT FORMAT(getdate(), 'dddd, MMMM dd, yyyy');

Output: Tuesday, December 10, 2019

ddd, MMMM dd, yyyy
SELECT FORMAT(getdate(), 'ddd, MMMM dd, yyyy');

Output: Tue, December 10, 2019

dd MMM yyyy
declare @date datetime = '2019-01-01'
SELECT FORMAT(@date, 'dd MMM yyyy');

Output: 01 Jan 2019

d MMM yyyy
declare @date datetime = '2019-01-01'
SELECT FORMAT(@date, 'd MMM yyyy');

Output: 1 Jan 2019

dd/MM/yy
SELECT FORMAT(getdate(), 'dd/MM/yy');

Output: 10/12/19

hh:mm:ss tt
SELECT FORMAT(getdate(), 'hh:mm:ss tt');

Output: 03:06:05 PM

h:mm:ss tt
SELECT FORMAT(getdate(), 'h:mm:ss tt');

Output: 3:06:05 PM

h:m:ss tt
SELECT FORMAT(getdate(), 'h:m:ss tt');

Output: 3:6:05 PM

MM/dd/yyyy hh:mm:ss
SELECT FORMAT(getdate(), 'MM/dd/yyyy hh:mm:ss');

Output: 12/10/2019 03:08:29

 

Also, check How To Use Date Functions In SQL

Yasin Panwala

Yasin Panwala is a Web Developer and Author at TheCodeHubs. He has experience in Web Developing and Designing and also in Writing. He has got his skills in working on technologies like .NET Core, ADO.NET, AJAX, Angular, AngularJS, ASP.NET, ASP.NET MVC, Bootstrap, C#, CSS, Entity Framework, Express.js, GraphQL, HTML, JavaScript, JQuery, JSON, LINQ, Microsoft Office, MongoDB, MySQL, Node.js, PostgreSQL, SQL, SQL Server, TypeORM, TypeScript, Visual Basic .NET, Web API. He also got his skills in working with different integration and some known versioning tools. He is always ready to learn new things and he always tries his best on tasks that are assigned to him and gives the best possible outputs.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago