我想转换一个像这样的字符串:
'10/15/2008 10:06:32 PM'
转换为Sql Server中的DATETIME值。
在甲骨文,我会这样说:
TO_DATE('10/15/2008 10:06:32 PM','MM/DD/YYYY HH:MI:SS AM')
这个问题意味着我必须将字符串解析为一种标准格式,然后使用其中一种代码进行转换。对于这样一个平凡的行动来说,这似乎很荒谬。有没有更简单的方法?
我想转换一个像这样的字符串:
'10/15/2008 10:06:32 PM'
转换为Sql Server中的DATETIME值。
在甲骨文,我会这样说:
TO_DATE('10/15/2008 10:06:32 PM','MM/DD/YYYY HH:MI:SS AM')
这个问题意味着我必须将字符串解析为一种标准格式,然后使用其中一种代码进行转换。对于这样一个平凡的行动来说,这似乎很荒谬。有没有更简单的方法?
当前回答
好评最多的答案是guravg和Taptronic的。然而,有一件事我想做。
它们显示的从0到131的具体格式数字可能会根据您的用例而有所不同(请参阅此处的完整数字列表),输入数字可能是不确定的,这意味着预期的结果日期在一个SQL SERVER实例到另一个实例之间是不一致的,出于同样的原因避免使用转换为字符串的方法。
从SQL Server 2005开始,其兼容性级别为90, 隐式日期转换变得不确定。日期转换 开始依赖于SET LANGUAGE和SET DATEFORMAT 90级。
非确定性值为0-100、106、107、109、113、130。这可能会导致错误。
最好的选择是坚持一个确定性的设置,我目前的首选是ISO格式(12、112、23、126),因为它们似乎是IT人员用例的最标准。
Convert(varchar(30), '210510', 12) -- yymmdd
Convert(varchar(30), '20210510', 112) -- yyyymmdd
Convert(varchar(30), '2021-05-10', 23) -- yyyy-mm-dd
Convert(varchar(30), '2021-05-10T17:01:33.777', 126) -- yyyy-mm-ddThh:mi:ss.mmm (no spaces)
其他回答
If you want SQL Server to try and figure it out, just use CAST CAST('whatever' AS datetime) However that is a bad idea in general. There are issues with international dates that would come up. So as you've found, to avoid those issues, you want to use the ODBC canonical format of the date. That is format number 120, 20 is the format for just two digit years. I don't think SQL Server has a built-in function that allows you to provide a user given format. You can write your own and might even find one if you search online.
SQL Server(2005, 2000, 7.0)没有任何灵活的,甚至是不灵活的方法来获取任意结构的字符串格式的datetime并将其转换为datetime数据类型。
所谓“随意”,我指的是“一种写作者(虽然可能不是你、我或地球另一边的人)认为是直观和完全明显的形式。”坦率地说,我不确定是否有这样的算法。
我花了一分钟才弄明白,所以在这里它可能会帮助到别人:
在SQL Server 2012和更好的版本中,你可以使用这个函数:
SELECT DATEFROMPARTS(2013, 8, 19);
下面是我如何将日期的部分提取到这个函数中:
select
DATEFROMPARTS(right(cms.projectedInstallDate,4),left(cms.ProjectedInstallDate,2),right( left(cms.ProjectedInstallDate,5),2)) as 'dateFromParts'
from MyTable
通过您的查询处理器运行它。它格式化日期和/或时间,其中一个应该会给你你要找的东西。适应并不难:
Declare @d datetime
select @d = getdate()
select @d as OriginalDate,
convert(varchar,@d,100) as ConvertedDate,
100 as FormatValue,
'mon dd yyyy hh:miAM (or PM)' as OutputFormat
union all
select @d,convert(varchar,@d,101),101,'mm/dd/yy'
union all
select @d,convert(varchar,@d,102),102,'yy.mm.dd'
union all
select @d,convert(varchar,@d,103),103,'dd/mm/yy'
union all
select @d,convert(varchar,@d,104),104,'dd.mm.yy'
union all
select @d,convert(varchar,@d,105),105,'dd-mm-yy'
union all
select @d,convert(varchar,@d,106),106,'dd mon yy'
union all
select @d,convert(varchar,@d,107),107,'Mon dd, yy'
union all
select @d,convert(varchar,@d,108),108,'hh:mm:ss'
union all
select @d,convert(varchar,@d,109),109,'mon dd yyyy hh:mi:ss:mmmAM (or PM)'
union all
select @d,convert(varchar,@d,110),110,'mm-dd-yy'
union all
select @d,convert(varchar,@d,111),111,'yy/mm/dd'
union all
select @d,convert(varchar,@d,12),12,'yymmdd'
union all
select @d,convert(varchar,@d,112),112,'yyyymmdd'
union all
select @d,convert(varchar,@d,113),113,'dd mon yyyy hh:mm:ss:mmm(24h)'
union all
select @d,convert(varchar,@d,114),114,'hh:mi:ss:mmm(24h)'
union all
select @d,convert(varchar,@d,120),120,'yyyy-mm-dd hh:mi:ss(24h)'
union all
select @d,convert(varchar,@d,121),121,'yyyy-mm-dd hh:mi:ss.mmm(24h)'
union all
select @d,convert(varchar,@d,126),126,'yyyy-mm-dd Thh:mm:ss:mmm(no spaces)'
dateadd(day,0,'10/15/2008 10:06:32 PM')