如表1  列1
基本要求
隔离栅和防落网实测项目
高度(mm)
镀(涂)层厚度(μm)
网面平整度(mm)
立柱埋深
立柱中距(mm)
混凝土强度(MPa)
立柱竖直度(mm/m)怎么样能只取每个每个字段括号里的单位,如果没有单位的就取空值,要查询的结果如下
表2
列1
null
null
 mm
 μm
 mm
null
 mm
 MPa
mm/m

解决方案 »

  1.   

    select substring(列1,charindex('(',列1)+1,charindex(')',列1 - 1) from tb
      

  2.   

    select case when charindex('(',列1) > 0 then substring(列1 , charindex('(',列1)+1 , charindex(')',列1) - charindex('(',列1) - 1 ) else null end as 列1 from tb/*
    列1                                                 
    -------------------------------------------------- 
    NULL
    NULL
    mm

    mm
    mm
    MPa
    mm/m(所影响的行数为 8 行)*/
      

  3.   

    服务器: 消息 245,级别 16,状态 1,行 1
    将 varchar 值 '总则' 转换为数据类型为 int 的列时发生语法错误。
      

  4.   

    drop table 表1
    go
    create table 表1(列1 varchar(100))
    insert into 表1
    select '基本要求'
    union select '隔离栅和防落网实测项目'
    union select '高度(mm)'
    union select '镀(涂)层厚度(μm)'
    union select '网面平整度(mm)'
    union select '立柱埋深'
    union select '立柱中距(mm)'
    union select '混凝土强度(MPa)'
    union select '立柱竖直度(mm/m)'select case when charindex('(',reverse(列1))>0 then reverse(substring(reverse(列1),2,charindex('(',reverse(列1))-2)) else NULl end from 表1 /*
    μm
    mm
    NULL
    MPa
    NULL
    NULL
    mm/m
    mm
    mm(所影响的行数为 9 行)
    */
      

  5.   

    Create Table 表1
    (列1 Nvarchar(50))
    Insert 表1 Select N'基本要求'
    Union All Select N'隔离栅和防落网实测项目'
    Union All Select N'高度(mm)'
    Union All Select N'镀(涂)层厚度(μm)'
    Union All Select N'网面平整度(mm)'
    Union All Select N'立柱埋深'
    Union All Select N'立柱中距(mm)'
    Union All Select N'混凝土强度(MPa)'
    Union All Select N'立柱竖直度(mm/m)'
    GO
    Select
    (Case When CharIndex(')', REVERSE(列1)) > 0 Then REVERSE(Substring(REVERSE(列1), CharIndex(')', REVERSE(列1)) + 1 , CharIndex('(', REVERSE(列1)) - CharIndex(')', REVERSE(列1)) - 1))
    Else Null End) As 列1
    From
    表1
    GO
    Drop Table 表1
    --Result
    /*
    列1
    NULL
    NULL
    mm
    μm
    mm
    NULL
    mm
    MPa
    mm/m
    */
      

  6.   

    Create Table 表1
    (列1 Nvarchar(50))
    Insert 表1 Select N'基本要求'
    Union All Select N'隔离栅和防落网实测项目'
    Union All Select N'高度(mm)'
    Union All Select N'镀(涂)层厚度(μm)'
    Union All Select N'网面平整度(mm)'
    Union All Select N'立柱埋深'
    Union All Select N'立柱中距(mm)'
    Union All Select N'混凝土强度(MPa)'
    Union All Select N'立柱竖直度(mm/m)'
    GO
    --得到单位
    Select
    (Case When CharIndex(')', REVERSE(列1)) > 0 Then REVERSE(Substring(REVERSE(列1), 2 , CharIndex('(', REVERSE(列1)) - 2))
    Else Null End) As 列1
    From
    表1--去掉括号跟单位
    Select
    (Case When CharIndex(')', REVERSE(列1)) > 0 Then REVERSE(Stuff(REVERSE(列1), 1, CharIndex('(', REVERSE(列1)), ''))
    Else 列1 End) As 列1
    From
    表1
    GO
    Drop Table 表1
    --Result
    /*
    列1
    NULL
    NULL
    mm
    μm
    mm
    NULL
    mm
    MPa
    mm/m列1
    基本要求
    隔离栅和防落网实测项目
    高度
    镀(涂)层厚度
    网面平整度
    立柱埋深
    立柱中距
    混凝土强度
    立柱竖直度
    */
      

  7.   

    create table tb2(unit varchar(50))insert into tb2 values ('王wdwadw(mm)')
    insert into tb2 values ('efeafe(yy)')
    insert into tb2 values ('feakfe(kk)fdas')
    insert into tb2 values ('feakfdafe(dd)fdas(cc)')
    insert into tb2 values ('fdasfdas')
    select * from tb2select reverse( substring(reverse(unit),2,len(unit)-charindex('(', reverse(unit)))) 
    from  tb2
    select  case when charindex(')',substring(reverse(unit),1,1))>0 
    then substring(reverse(unit),2,charindex('(',reverse(unit))-2) else null end
    from tb2
      

  8.   

    好象我楼上的楼上没考虑到fdasfdasfdsa(mm)fdsafds这种情况
    LZ 给的数据也没有这种情况