I have a varchar
column in a table that contains data that looks like this:
2010-06-24-00.00.00.000000
I need to convert it to a Date
that looks like this:
6/24/2010
Using T-SQL. How can I do this? I'm using SQL Server 2012.
What date format is this? 2010-06-24-00.00.00.000000
You can use the FORMAT function:
FORMAT(datefield,'MM/dd/yyyy')
But, since it's a VARCHAR, to use the FORMAT function you have to first CAST the date portion as a date:
FORMAT(CAST(LEFT(stringdatefield,10) AS DATE),'MM/dd/yyyy')
select convert(date, left('2010-06-24-00.00.00.000000', 10))
You can add a optional parameter to the convert function. If you want the result to be a string, simply do this:
CONVERT(varchar(8),datefield,1)
If you want the result to be a DATE type, cast it:
CAST(CONVERT(varchar(8),datefield,1) as DATE)
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.