SQL Server - 如何在'/'符号后将第一个下一个char转换为大写

时间:2013-05-13 04:34:42

标签: sql-server

我有一个问题,关于如何在字符串中的'/'符号后将第一个下一个char转换为大写。

举个例子:

CustomerNameramayanan s/o vicky ratnam

首先,我需要它在适当的情况下,所以我使用它:

Update
dbo.table set CustomerName = dbo.ProperCase(CustomerName)

结果:Ramayanan S/o Vicky Ratnam

但我需要它为:Ramayanan S/O Vicky Ratnam/之后的第一个字符应位于uppercase

4 个答案:

答案 0 :(得分:1)

UPDATE dbo.[table]
SET CustomerName = REPLACE(ProperCase(CustomerName),
                           SUBSTRING(CustomerName, CHARINDEX('/',CustomerName),2),
                           UPPER(SUBSTRING(CustomerName, CHARINDEX('/',CustomerName),2)))
WHERE CHARINDEX('/',CustomerName) > 0;

搜索拳/并将/和后续字符替换为/,将以下字符替换为大写字母。

答案 1 :(得分:1)

请检查:

declare @T table(Insurance varchar(max))

insert into @T values ('roeselare')
insert into @T values ('BRUGGE')
insert into @T values ('ramayanan s/o vicky ratnam')

select (
       select upper(T.N.value('.', 'char(1)'))+
                lower(stuff(T.N.value('.', 'varchar(max)'), 1, 1, ''))+(CASE WHEN RIGHT(T.N.value('.', 'varchar(max)'), 1)='/' THEN '' ELSE ' ' END)
       from X.InsXML.nodes('/N') as T(N)
       for xml path(''), type
       ).value('.', 'varchar(max)') as Insurance
from 
  (
  select cast('<N>'+replace(
            replace(
                Insurance, 
                '/', '/</N><N>'),
            ' ', '</N><N>')+'</N>' as xml) as InsXML
  from @T
  ) as X

答案 2 :(得分:0)

Update dbo.table set CustomerName = REPLACE(CustomerName ,'/o','/O')

答案 3 :(得分:0)

    Drop Table #Temp
    Create Table #Temp
    (
    CustomerName varchar(100)
    ) 

    Insert into #Temp
    values('Ramayanan S/o vicky ratnam')

    Select * from #temp

    Select  UPPER(SUBSTRING(CustomerName, CHARINDEX('/',CustomerName),2)),REPLACE( CustomerName ,
                               SUBSTRING(CustomerName, CHARINDEX('/',CustomerName),2),
                               UPPER(SUBSTRING(CustomerName, CHARINDEX('/',CustomerName),2))) from #temp


    UPDATE dbo.#Temp
    SET CustomerName = REPLACE( CustomerName ,
                               SUBSTRING(CustomerName, CHARINDEX('/',CustomerName),2),
                               UPPER(SUBSTRING(CustomerName, CHARINDEX('/',CustomerName),2))); 

     Select * from #temp