[英]Read nvarchar data into DateTimeOffset in T-SQL
I have nvarchar
values with the following format我有以下格式的nvarchar
值
10:27:32.357 +03 Aug 31 2022
How to convert those to DatetimeOffset
variables in T-SQL?如何将这些转换为 T-SQL 中的DatetimeOffset
变量?
I have to deal with huge amounts of data, so I want to avoid rebuild the value as segments and then use DATETIMEOFFSETFROMPARTS.我必须处理大量数据,所以我想避免将值重建为段,然后使用 DATETIMEOFFSETFROMPARTS。
Based on the one example we have, and assuming your dates are always in the format hh:mm:ss.nnn [+-]tzoffset MMM dd yyyy
(which is honestly an awful format) you could do something like this:基于我们拥有的一个示例,并假设您的日期始终采用hh:mm:ss.nnn [+-]tzoffset MMM dd yyyy
格式(这确实是一种糟糕的格式),您可以执行以下操作:
DECLARE @BadDateString nvarchar(50) = N'10:27:32.357 +03 Aug 31 2022';
SELECT CONVERT(datetimeoffset(3),CONVERT(varchar(10),CONVERT(date,RIGHT(@BadDateString,11)),120)+'T'+REPLACE(LEFT(@BadDateString,16),' ','') + ':00')
I strongly suggest, however, that you fix the problem by having the application that is sending this data in this format as a strongly typed date and time ( datetimeoffset
) value in the first place;但是,我强烈建议您首先让以这种格式发送此数据的应用程序作为强类型日期和时间 ( datetimeoffset
) 值来解决问题; T-SQL's forté isn't strong at string manipulations like like. T-SQL 的长处不擅长像 like 这样的字符串操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.