Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a datetime to string in T-SQL

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.)

like image 704
cja Avatar asked Feb 22 '13 17:02

cja


People also ask

How do I convert DateTime to string?

Convert DateTime to String using the ToString() Method Use the DateTime. ToString() method to convert the date object to string with the local culture format. The value of the DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.


1 Answers

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)  
  • SQLFiddle Demo
  • SQL Server Date Formats
like image 115
John Woo Avatar answered Oct 06 '22 11:10

John Woo