if(15位)
substring('xxxx',7,6)
if(18位)
substring('xxxx',7,8)
else(其他)
解决方案 »
- 简单统计为何不行???
- SQL数据库复制指定字段内容到另一个数据库里的问题
- sqlserver2008 r2,无智能提示,怎么显示智能提示
- 高分求教:在触发器中使用链接服务器的问题
- 求助,怎样实现下图流程中的sql语句?
- Microsoft OLE DB Provider for ODBC Drivers 错误 '80004005'
- 如何在preparestatement 中使用 in 条件
- 这个SQL语句怎么写
- 求一个sql语句
- 求助:怎样把EXEC执行后的结果集赋给一个参数变量???
- 请教:sql语句问题(急用!)
- 在ms-sql2000中新添的一个用户Vodmanager如何将其添加到服务器角色system adiministrators中?
SUBSTRING ( expression , start , length ) 参数
expression
是字符串、二进制字符串、text、image、列或包含列的表达式。不要使用包含聚合函数的表达式。
start
是一个整数,指定子串的开始位置。
length
是一个整数,指定子串的长度(要返回的字符数或字节数)。
if(@weishu=15)
LEN
返回给定字符串表达式的字符(而不是字节)个数,其中不包含尾随空格。语法
LEN ( string_expression ) 参数
string_expression要计算的字符串表达式。返回类型
int
substring('xxxx',7,6)
else if(len('xxxx')=18)
substring('xxxx',7,8)
else
有一张表
出生日期 身份证号
null 510122198912082373(18位)
null 5101228912082373(15位)
我要把出生日期提出来,这个update语句怎么写?
update ××
set 出生日期=substring(身份证号,7,8)
这个就没有判断身份证的位数,15位的就会出现问题。
这个应该怎么解决
when 15 then substring(身份证号,7,6)
case when len(rtrim(身份证号))=15 then substring(身份证号,7,6)
when len(rtrim(身份证号))=18 then substring(身份证号,7,8) end如果出生日期是datetime类型,再转换一下
update yourtable set 出生日期=case len(身份证号) when 18 then substring(身份证号,7,8)
when 15 then substring(身份证号,7,6) end
case when len(rtrim(身份证号))=15 then '年份'+substring(身份证号,7,6)
when len(rtrim(身份证号))=18 then substring(身份证号,7,8) end
CREATE function Get_Birthday_date
(@idcard varchar(18))
returns datetime
--returns varchar(10)
as
begin
declare @Idlen numeric(2,0),
@birth varchar(10)
set @idlen=len(@idcard)
If @idlen=15 and SUBSTRING(@idcard,7,2)>0 and SUBSTRING(@idcard,9,2)< 13 and
SUBSTRING(@idcard,11,2) <32
set @birth='19'+SUBSTRING(@idcard,7,2)+'-'+SUBSTRING(@idcard,9,2)+'-'+SUBSTRING(@idcard,11,2)if @idlen=18 and SUBSTRING(@idcard,11,2) < 13 and SUBSTRING(@idcard,13,2)< 32
and SUBSTRING(@idcard,7,4)> 0set @birth=SUBSTRING(@idcard,7,4)+'-'+SUBSTRING(@idcard,11,2)+'-'+SUBSTRING(@idcard,13,2)
If len(@birth) <> 10
set @birth='1900-01-01'
return cast( convert(char(10),@birth) as datetime)
--return @birth
end