手上有一张表xx,表里含有2个字段,一个是出生日期(csrq),数据类型为datetime,一个是身份证号码(sfzhm),数据类型为varchar,,现在出生年月这个字段里面是空的(null),我现在想把身份证号里面的出生日期提取出来,放进出生年月这个字段里,但是不知道该如何写语句,这里面还隐含一个条件,因为身份证号码分15位和18位两种。还要把空字段继续保留为NULL
要使用SQL查询分析器完成以上操作。。
另外:
update xx set csrq=convert(datetime,substring(sfzhm,7,8)) from xx where len(sfzhm)=18
会报错,信息如下:
服务器: 消息 8115,级别 16,状态 2,行 1
Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.
使用了2楼的语句,出错误。
服务器: 消息 242,级别 16,状态 3,行 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.
update xx set csrq=substring(sfzhm,7,8) from xx where len(sfzhm)=18update xx set csrq='19'+substring(sfzhm,7,6) from xx where len(sfzhm)=15 服务器: 消息 242,级别 16,状态 3,行 1The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.The statement has been terminated.------------