Warm tip: This article is reproduced from serverfault.com, please click

How to convert a datetime to string in T-SQL

发布于 2013-02-22 17:24:34

I'm surprised not to be able to find this question here already.

I have a date time var and I want to convert it to a string so that I can append it to another string. I want it in a format that can be converted easily back to a date time.

How can I do this?

(I want the date part and the time part.)

Questioner
cja
Viewed
12
John Woo 2013-02-23 01:25:38

The following query will get the current datetime and convert into string. with the following format
yyyy-mm-dd hh:mm:ss(24h)

SELECT convert(varchar(25), getdate(), 120)