How to convert a datetime to string in T-SQL

前端 未结 9 1366
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 03:19

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 stri

9条回答
  •  庸人自扰
    2020-12-13 03:43

    You can use the convert statement in Microsoft SQL Server to convert a date to a string. An example of the syntax used would be:

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

    The above would return the current date and time in a string with the format of YYYY-MM-DD HH:MM:SS in 24 hour clock.

    You can change the number at the end of the statement to one of many which will change the returned strings format. A list of these codes can be found on the MSDN in the CAST and CONVERT reference section.

提交回复
热议问题