如何将字符串ddmmyyyyhhmi转换为yyyy-mm-dd ?
字符串210420161701 ( varchar )进入2016-04-21 ( datetime数据类型)。
我尝试了子串,转换,转换的组合,但似乎无法使其工作。
请帮助谢谢。
how can I convert a string ddmmyyyyhhmi into yyyy-mm-dd?
A string 210420161701 (a varchar) into 2016-04-21 (a datetime data type).
I tried a combination of sub-string, cast, convert but can't seem to make it work.
Please kindly help thanks.
最满意答案
前两行只是设置变量,但以下工作(只要格式始终相同)。 您可能还需要确保SQL安装的日期格式正确(SET DATEFORMAT)。 如果需要,您还可以将数据类型从日期更改为日期时间。
DECLARE @Date Varchar(100) = '210420161701' DECLARE @DateVariable date SELECT @DateVariable = SUBSTRING(@date,5,4) + '-' + SUBSTRING(@date,3,2) + '-' + SUBSTRING(@date,1,2) SELECT @DateVariableThe first 2 lines are just setting up the variable, but the following works (as long as the format is always the same). You may also need to make sure the date format of your SQL installation is correct (SET DATEFORMAT). You can also change the datatype from date to datetime if required.
DECLARE @Date Varchar(100) = '210420161701' DECLARE @DateVariable date SELECT @DateVariable = SUBSTRING(@date,5,4) + '-' + SUBSTRING(@date,3,2) + '-' + SUBSTRING(@date,1,2) SELECT @DateVariable更多推荐
发布评论