开发者

SQL Converting character to datetime

开发者 https://www.devze.com 2023-02-10 04:23 出处:网络
Does anybody now how to 开发者_开发技巧convert this character to date or datetime: \'10022011\'Quick & dirty:

Does anybody now how to 开发者_开发技巧convert this character to date or datetime: '10022011'


Quick & dirty:

select convert(datetime, stuff(stuff('10022011', 5,0,'-'),3,0,'-'))

However, you might want to consider converting the string into ISO standard date format:

declare @d char(8)
select @d = '10022011'

select convert(datetime, substring(@d,5,4) + '-' + substring(@d,3,2) + '-' + substring(@d, 1, 2))

in order to avoid ambiguity. 10-02-2011 has different meanings depending on which side of the pond you are.


SELECT convert(datetime, STUFF(STUFF('10022011',3,0,'-'),6,0,'-'), 103) 

the number at the end is the Sql format you want the date to output

0

精彩评论

暂无评论...
验证码 换一张
取 消