Tuesday, September 3, 2013

Query to get only date from datetime in sql server like dd/mm/yyyy, mm/dd/yyyy, yyyymmdd, dd-mm-yyyy etc

Introduction

Here I will explain how to convert or get only date from datetime in different formats like like dd/mm/yyyy, mm/dd/yyyy, yyyymmdd, dd-mm-yyyy etc in SQL Server.

Description

In many situations I got requirement like changing datetime to date and changing the date format to different ways like dd/mm/yyyy or mm/dd/yyyy or dd.mm.yy or yy.mm.dd. We can covert our datetime to date with different formats using different styles. I will display different format of dates with samples check it 



DECLARE @DateTime DATETIME

SELECT @DateTime = '2010/05/20 11:21:13'

/*For mm/dd/yyyy format*/
Select CONVERT(VARCHAR(10),@DateTime ,101) as Date








/*For yyyy.mm.dd format*/
Select CONVERT(VARCHAR(10),@DateTime ,102) as Date








/*For dd/mm/yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,103) AS Date








/*For dd.mm.yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,104) AS Date








/*For dd-mm-yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,105) AS Date








/*For hh:mi:ss format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,108) AS Date








/*For mm-dd-yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,110) AS Date








/*For yyyy/mm/dd format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,111) AS Date








/*For yyyymmdd format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,112) AS Date


Source : http://www.aspdotnet-suresh.com/2010/11/how-to-convert-datetime-to-date-in.html

No comments:

Post a Comment