Sql server 字符串到日期转换问题SQL
我对以下转换存在问题: 我使用的是两个主表client和cli identifier(这包含信息列) 我在cli标识符表中有一个名为information的列,它当前以两种方式保存日期(唯一改变的是日期值): 我使用以下查询从列中分割信息:Sql server 字符串到日期转换问题SQL,sql-server,codeigniter,date,Sql Server,Codeigniter,Date,我对以下转换存在问题: 我使用的是两个主表client和cli identifier(这包含信息列) 我在cli标识符表中有一个名为information的列,它当前以两种方式保存日期(唯一改变的是日期值): 我使用以下查询从列中分割信息: left(Substring(ci.information, charindex('[0-9]',ci.information),103) ,charindex('*',ci.information + '*')-2) as datereceiv
left(Substring(ci.information, charindex('[0-9]',ci.information),103)
,charindex('*',ci.information + '*')-2) as datereceived
之后,我收到以下字符串:
我需要这些转换的日期/时间或仅日期,但无法使用以下代码执行此操作(我也尝试使用cast来执行此操作):
我尝试过的其他方法是将其转换为临时表,然后使用以下方法强制转换日期
select
cast (t AS date()
from #a
我知道冲突在于日期值被格式化为22/11/16任何输入都会有帮助。我想到的唯一解决方案是添加到临时表中,并进行查询以在22/11/16年份的前面插入“20”值。如果您使用的是
SQL Server
,您可以这样做,只需检测日期字符串的长度并使用样式3或103即可
select CONVERT( DATETIME , '22/11/16', 3),
CONVERT( DATETIME , '22/11/2016', 103)
如果您使用的是
SQL Server
,您可以这样做,只需检测日期字符串的长度并使用样式3或103即可
select CONVERT( DATETIME , '22/11/16', 3),
CONVERT( DATETIME , '22/11/2016', 103)
好的,谢谢。
我添加了以下内容来计算字符串的长度
case when len(left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2)) >= 10
then convert(date,left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2),103)
else convert(date,left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2),3)
end as datereceived
问候,好的,谢谢。
我添加了以下内容来计算字符串的长度
case when len(left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2)) >= 10
then convert(date,left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2),103)
else convert(date,left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2),3)
end as datereceived
关于,是sql还是mysql?抱歉,只是sql添加了错误的标记。是sql还是mysql?抱歉,只是sql添加了错误的标记。
case when len(left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2)) >= 10
then convert(date,left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2),103)
else convert(date,left(Substring(ci.information, charindex('[0-9]',ci.information),103),charindex('*',ci.information + '*')-2),3)
end as datereceived