t1表,c1字段的数据是多行的:"中国林业出版社, 2000
840页 ;
7-5038-2336-4"现在要把840这个页数提出来赋给c2。
估计写SQL用正则不太好弄,我只知道正则表达式可以在like里用。
那就是截取换行符和"页"之间的字符串了,望高手指教!

解决方案 »

  1.   

    declare @t table(col varchar(50))
    insert @t select '中国林业出版社,   2000 
    840页   ; 
    7-5038-2336-4'
    select * from @tselect col ,substring(col,charindex('
    ',col)+2,charindex(';',col) - charindex('
    ',col))  as col2 from @t
    /*col                                                col2                                               
    -------------------------------------------------- -------------------------------------------------- 
    中国林业出版社,   2000 
    840页   ; 
    7-5038-2336-4                                       840页   ; (所影响的行数为 1 行)
    */
      

  2.   

    declare @t table(col varchar(50))
    insert @t select '中国林业出版社,   2000 
    840页   ; 
    7-5038-2336-4'
    select * from @tselect col ,substring(col,charindex('
    ',col)+2,charindex('页',col) - charindex('
    ',col) - 2)  as col2 from @t
    /*col                                                col2                                               
    -------------------------------------------------- -------------------------------------------------- 
    中国林业出版社,   2000 
    840页   ; 
    7-5038-2336-4                                       840 (所影响的行数为 1 行)
    */
      

  3.   

    declare @t table(col varchar(50))
    insert @t select '中国林业出版社,   2000 
    840页   ; 
    7-5038-2336-4'select 
    col,
    rtrim(substring(col,charindex(char(10),col),charindex(';',col)-charindex(char(10),col))) as col2
    from 
    @T
      

  4.   

    呵呵,经测试,应该用 CHAR(10)+CHAR(13)
      

  5.   

    到底什么时候用CHAR(10),什么时候用CHAR(13),什么时候用CHAR(10)+CHAR(13)啊
    我是生试出来的。。另外我的数据是从Excel里导出来的。。
      

  6.   

    真想拿正则直接把它匹配出来。。好像还是写个PHP省事。。
      

  7.   


    正则表达式可以的declare @t table(AllName varchar(100))
    insert @t select '中国林业出版社,   2000 
    840页   ; 
    7-5038-2336-4'
    insert @t select '中国林
    业出版社,   2000 
    840页   ; 
    7-5038-2336-4'
    select AllName,rtrim(substring(AllName,PATINDEX('%['+ char(10)+char(13) +'][0-9]%页%',AllName)+1,charindex('页',AllName)-PATINDEX('%['+ char(10)+char(13) +'][0-9]%页%',AllName)-1))  as col2 
    from @t