SQL Server 将字符串转换为日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1135746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:49:19  来源:igfitidea点击:

SQL Server convert string to datetime

sqlsql-serverstringdatetime

提问by NeilG

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

这不是问如何在 MSSQL 中将任意字符串转换为日期时间,例如这个问题

I can control the string format but I want to know what the MSSQL syntax is for updating a datetime field using a date string.

我可以控制字符串格式,但我想知道使用日期字符串更新日期时间字段的 MSSQL 语法是什么。

采纳答案by NeilG

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 firstthen there is no problem; date order will always be year-month-day.

您需要注意日/月顺序,因为当未首先指定年份时,这将取决于语言。如果您先指定年份,则没有问题;日期顺序将始终是年-月-日。

回答by Bork Blatt

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:

有关 CAST 和 CONVERT 的完整讨论,包括不同的日期格式选项,请参阅下面的 MSDN 库链接:

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

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