用户导入EXCEL数据,用一个存储过程来执行,需要判断其中一列的数据格式,nvchar类型,可以为null值,或全部是数字或是形如"xxx-xxx-xxx"的格式,其中x为数字,且每一部分xxx的数字位数是不固定的,想用正则表达式来做判断,不知道该怎么写啊?

解决方案 »

  1.   

    where col is null 
        or (col not like '%[^0-9-]%'
    and len(col) - len(replace(col,'-','')) in(0,2))
      

  2.   

    select *
    from (
    select col = null union all
    select col = 'abc' union all
    select col = 'a7bc' union all
    select col = '999' union all
    select col = '9-2-3' union all
    select col = '9-a-3' union all
    select col = '9-2-2-2' union all
    select col = '9-a-2-2' 
    )a
    where col is null
    or (col not like '%[^0-9-]%'
    and len(col) - len(replace(col,'-','')) in(0,2))-- 结果:
    NULL
    999
    9-2-3
      

  3.   

    sql server 不直接支持正则表达式, 所以在楼主的需求中, like 匹配更适合
      

  4.   

    sql里的正则表达式不知道怎么处理
    但是针对这个问题可以这么做:
    if isnumeric(replace('xxx-xxx-xxx','-','')) = 1
      print '数字'