convert转换时间类型出错

zwfgdlc 2011-08-08 12:24:07
select convert(datetime, '19'+substring(sidcard,7,6)) from table
where len(sidcard)

sidcard是身份证号字段,
把15位的身份证号码加上世纪数转换datetime类型时出错.
提示值越界。
...全文
206 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
geniuswjt 2011-08-08
  • 打赏
  • 举报
回复
多打了个from - -[Quote=引用 11 楼 geniuswjt 的回复:]
估计有异常数据,你

SQL code
select * from from table
where len(sidcard)!=datalength(sidcard) --试下,结果贴出来看看?


引用 9 楼 zwfgdlc 的回复:
可能是身份证号码的问题,直接用用字符串替代字段可以转换
[/Quote]
oO寒枫Oo 2011-08-08
  • 打赏
  • 举报
回复
嗯 2月29生日在作怪 或者有 3月32日
geniuswjt 2011-08-08
  • 打赏
  • 举报
回复
估计有异常数据,你
select * from from table
where len(sidcard)!=datalength(sidcard) --试下,结果贴出来看看?
[Quote=引用 9 楼 zwfgdlc 的回复:]
可能是身份证号码的问题,直接用用字符串替代字段可以转换
[/Quote]
AcHerat 元老 2011-08-08
  • 打赏
  • 举报
回复
看看存身份证号码那个字段的数据有没有全角输入的数字。
zwfgdlc 2011-08-08
  • 打赏
  • 举报
回复
可能是身份证号码的问题,直接用用字符串替代字段可以转换
geniuswjt 2011-08-08
  • 打赏
  • 举报
回复
select convert(datetime,('19'+substring(sidcard,7,2)+'-'+substring(sidcard,9,2)+'-'+substring(sidcard,11,2))) from table
where len(sidcard)=15
少打了个“)”,更正下[Quote=引用 5 楼 geniuswjt 的回复:]
SQL code
select convert(datetime,('19'+substring(sidcard,7,2)+'-'+substring(sidcard,9,2)+'-'+substring(sidcard,11,2)) from table
where len(sidcard)=15
[/Quote]
--小F-- 2011-08-08
  • 打赏
  • 举报
回复
那应该没什么问题啊
快溜 2011-08-08
  • 打赏
  • 举报
回复

select convert(datetime, '19'+substring('4416248309211351',7,6))

/*

-----------------------
1983-09-21 00:00:00.000

(1 行受影响)
geniuswjt 2011-08-08
  • 打赏
  • 举报
回复
select convert(datetime,('19'+substring(sidcard,7,2)+'-'+substring(sidcard,9,2)+'-'+substring(sidcard,11,2)) from table
where len(sidcard)=15
geniuswjt 2011-08-08
  • 打赏
  • 举报
回复
加个-
zwfgdlc 2011-08-08
  • 打赏
  • 举报
回复

select convert(datetime, '19'+substring(sidcard,7,6)) from table
where len(sidcard)=15

比如一个号码是:4416248309211351
83年9月21日出生的
要把830921转成19830921再转换成datetime类型的,用datediff来运算
快溜 2011-08-08
  • 打赏
  • 举报
回复
select '19'+substring(sidcard,7,6) from tb 

看看是否能转换为时间格式
--小F-- 2011-08-08
  • 打赏
  • 举报
回复
15位的号码是哪样的?
chtzhking 2011-08-08
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 chtzhking 的回复:]

第一:在时间超出时间范围
第二:在数据库中查询的省份证日期中的数据有些可能是0,1这一类的不是0,1也会报错 如下SQL code

select convert(datetime, '19'+substring('4416248309211351',7,6))
-----------------------
1983-09-21 00:00:00.000

(1 行受影响)

select……
[/Quote]将19楼代码复制到SQL中看区别
chtzhking 2011-08-08
  • 打赏
  • 举报
回复
第一:在时间超出时间范围
第二:在数据库中查询的省份证日期中的数据有些可能是0,1这一类的不是0,1也会报错 如下

select convert(datetime, '19'+substring('4416248309211351',7,6))
-----------------------
1983-09-21 00:00:00.000

(1 行受影响)

select convert(datetime, '19'+substring('4416248309211351',7,6))

-----------------------
消息 242,级别 16,状态 3,第 1 行
从 varchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值。

gogodiy 2011-08-08
  • 打赏
  • 举报
回复
建议先检查数据,看是否有不符合日期格式的存在。
Rock_Wu 2011-08-08
  • 打赏
  • 举报
回复
呵呵,我也在清洗数据的时候,发现很多32号的,呵呵
zwfgdlc 2011-08-08
  • 打赏
  • 举报
回复
找出问题了。的确是年月日有问题。
有些月份大于12月,有些日期大于31.
数据库之前坏过一次,有些数据乱了。
oO寒枫Oo 2011-08-08
  • 打赏
  • 举报
回复
但是84年的 2月29日是对的
可能身份证中确实有这样的数据 830229 这种是无法转化的

select convert(datetime, '19'+substring('4416248402291351',7,6))
oO寒枫Oo 2011-08-08
  • 打赏
  • 举报
回复

select convert(datetime, '19'+substring('4416248303321351',7,6))

select convert(datetime, '19'+substring('4416248302291351',7,6))

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧