Sql server 2005 使用isdate()将varchar转换为日期
我有一个平面文件,作为字符数据导入到SQLServer2005临时表中 将生日字段复制到最终目标表时,我需要将其转换为日期时间格式。我是通过以下方式进行的:Sql server 2005 使用isdate()将varchar转换为日期,sql-server-2005,Sql Server 2005,我有一个平面文件,作为字符数据导入到SQLServer2005临时表中 将生日字段复制到最终目标表时,我需要将其转换为日期时间格式。我是通过以下方式进行的: BIRTHDAY = case when isdate(DOB)=1 then convert(datetime, '19'+right(DOB, 2)+left(DOB, 2)+substring(DOB,3,2)) else null end 问题是32k+文件中只有100+个生日被标识为日期 我看不出有日期的和没有日期的有什么区别。
BIRTHDAY = case when isdate(DOB)=1 then convert(datetime, '19'+right(DOB, 2)+left(DOB, 2)+substring(DOB,3,2)) else null end
问题是32k+文件中只有100+个生日被标识为日期
我看不出有日期的和没有日期的有什么区别。我在下面包括了一个样本
good date bad date
41129 100465
10531 122467
10429 20252
81030 62661
31231 20959
11028 91965
80928 60665
看起来原始数据的单位是MMDDYY,但月份不是0填充的 基于此假设,您可以像下面这样解析日期部分并重新生成日期时间:
declare @raw table (dob varchar(100));
insert into @raw
select '41129' union all
select '10531' union all
select '10429' union all
select '81030' union all
select '31231' union all
select '11028' union all
select '80928' union all
select '100465' union all
select '122467' union all
select '20252' union all
select '62661' union all
select '20959' union all
select '91965' union all
select '60665'
select *,
[asDate] = dateadd(day, dd - 1, dateadd(month, mm - 1, dateadd(year, ('19' + yy)-1900, 0)))
from ( select dob,
substring(right('0' + dob, 6), 1, 2),
substring(right('0' + dob, 6), 3, 2),
substring(right('0' + dob, 6), 5, 2)
from @raw
) as stage (string, mm, dd, yy);