简体   繁体   中英

SQL Server Convert Time Stamp in date

Function to get time stamp to date format for example

Date Field
2014-06-15 00:00:00.000

I want it to show the date as 6/15/2014 in another column call day.

What function do i use in sql server?

Thanks

Select Convert(Varchar,Convert(Date,'2014-06-15 00:00:00.000'),110)

For SQL 2012 and above:

DECLARE @TimeStamp DATETIME = '2014-06-15 00:00:00.000'

SELECT FORMAT(@TimeStamp,'d','en-US') AS Format_Date

Results:

Format_Date
---------------
6/15/2014

If the source column is of type Datetime and target column can be of varchar then you can simply write as:

SELECT CONVERT(VARCHAR(10), mydatetimecolumn, 101) AS [MM/DD/YYYY]
From Testtable

else if source column is of type varchar and you are sure that all dates are valid then you can write as:

SELECT CONVERT(VARCHAR(10), cast('2014-06-15 00:00:00.000' as datetime),
101) AS [MM/DD/YYYY]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM