标签:
convert datetime 127 和 Stuff 一起使用时,需要慎重注意删除字符的start position
Convert Datetime to 127 的格式是 yyyy-mm-ddThh:mi:ss.mmm
MSDN给出一个注意事项:
When the value for milliseconds (mmm) is 0, the milliseconds value is not displayed. For example, the value ‘2012-11-07T18:26:20.000 is displayed as ‘2012-11-07T18:26:20‘.
示例
declare @dt1 datetime declare @dt2 datetime set @dt1=‘2015-01-02 01:23:45.678‘ set @dt2=‘2015-01-02 01:23:45.000‘ select CONVERT(VARCHAR(50),@dt1, 127),len(CONVERT(VARCHAR(50),@dt1, 127)), CONVERT(VARCHAR(50),@dt2, 127),len(CONVERT(VARCHAR(50),@dt2, 127))
如果 stuff( convert(varchar(50),@datetime,127),20,4) 会出现一个“意外”的结果
declare @dt1 datetime declare @dt2 datetime set @dt1=‘2015-01-02 01:23:45.678‘ set @dt2=‘2015-01-02 01:23:45.000‘ select CONVERT(VARCHAR(50),@dt1, 127),len(CONVERT(VARCHAR(50),@dt1, 127)), CONVERT(VARCHAR(50),@dt2, 127),len(CONVERT(VARCHAR(50),@dt2, 127)), STUFF(CONVERT(VARCHAR(50),min(@dt1), 127),20,4,‘‘) , STUFF(CONVERT(VARCHAR(50),min(@dt2), 127),20,4,‘‘)
原因是Stuff函数返回null,MSDN给出解释
STUFF ( character_expression , start , length , replaceWith_expression )
If the start position or the length is negative, or if the starting position is larger than length of the first string, a null string is returned. If the start position is 0, a null value is returned. If the length to delete is longer than the first string, it is deleted to the first character in the first string.
convert datetime 127 和 Stuff 一起使用时,需要慎重注意删除字符的start position
标签:
原文地址:http://www.cnblogs.com/ljhdo/p/4873094.html