有一个字段是城市名称 CityName, 需要根据大写字母插入空格,比如:
MiddleSackville 改为: Middle Sackville
NanooseBay      改为: Nanoose Bay 
OterRioTuey     改为: Oter Rio Tuey
我知道是用: update 表名 set 字段=replace(字段,'字符串','字符串') 这个查询语句,但是如何判断大写字母呢?
求高手给一个答案,有10万条记录,不可能手工去该的。多谢!!!

解决方案 »

  1.   

    可以用ASCII ( character_expression )来判断,大写字母的值介于65~90 小写字母介于97~122.搭配case when
      

  2.   

    通过ascii来比较select ascii('a'),ascii('A')
    /*
    ----------- -----------
    97          65
    */
      

  3.   

    unicode也可以select unicode('a'),unicode('A')
      

  4.   

    可以具体写一下查询语句吗?本人初学MSSQL
      

  5.   

    CASE WHEN ascii(字段) between 65 and 97 Then '大写' ELSE '小写',如何插进查询语句中去呢?
      

  6.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([col] varchar(100))
    insert [tb]
    select 'MiddleSackville' union all
    select 'NanooseBay' union all
    select 'OterRioTuey'
    go
    create function f_space(@s varchar(100)) 
    returns nvarchar(100) 
    as 
    begin
      declare @i int,@r varchar(100)
      set @i=1
      set @r=''
      while @i<=len(@s)
      begin
        set @r=@r+
               case 
                 when ascii(substring(@s,@i,1))!=ascii(lower(substring(@s,@i,1))) and @i!=1  
                   then ' '+substring(@s,@i,1) 
                 else 
                   substring(@s,@i,1) 
                end
        set @i=@i+1
      end
      return @r
    end
    goselect dbo.f_space(col) as col from tb/**
    col
    -----------------------------
    Middle Sackville
    Nanoose Bay
    Oter Rio Tuey(3 行受影响)
    **/--更新使用以下语句(字段长度与函数返回值一致)update tb set col=dbo.f_space(col)