SQL Server convert string to datetime

前端 未结 2 1251
名媛妹妹
名媛妹妹 2020-11-27 20:38

This is not asking how to convert an arbitrary string to datetime in MSSQL such as this question.

I can control the string format but I want to know what the MSSQL s

相关标签:
2条回答
  • 2020-11-27 21:18

    For instance you can use

    update tablename set datetimefield='19980223 14:23:05'
    update tablename set datetimefield='02/23/1998 14:23:05'
    update tablename set datetimefield='1998-12-23 14:23:05'
    update tablename set datetimefield='23 February 1998 14:23:05'
    update tablename set datetimefield='1998-02-23T14:23:05'
    

    You need to be careful of day/month order since this will be language dependent when the year is not specified first. If you specify the year first then there is no problem; date order will always be year-month-day.

    0 讨论(0)
  • 2020-11-27 21:26
    UPDATE MyTable SET MyDate = CONVERT(datetime, '2009/07/16 08:28:01', 120)
    

    For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:

    https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

    0 讨论(0)
提交回复
热议问题