当前位置: 代码迷 >> Sql Server >> 取前两个字母大写解决方案
  详细解决方案

取前两个字母大写解决方案

热度:55   发布时间:2016-04-27 11:09:42.0
取前两个字母大写
如 customer变成CU
如果两个单词就取单词首字母 customer code: CC

最后如果结果有重复则试第三个字母
project 和 product 可能就要变成 PR 和 PO


请大侠帮帮忙,谢谢啦


------解决方案--------------------
SQL code
if object_id('tempdb..#ta')is not null drop table #tacreate table #ta(name varchar(50))insert into #ta select 'customer code'insert into #ta select 'customer'insert into #ta select 'product'insert into #ta select 'product project'insert into #ta select 'product'insert into #ta select 'project';with CET1 as (select distinct name from #ta),CET2 as(select  name,upper(left(name,1)+ left(ltrim(stuff(name,1,charindex(' ',name),'')),1 )) as Up         from CET1 where ltrim(rtrim(name)) like '% %'union allselect  name,Upper(left(name,1) + substring(name,1 + row_number() over(partition by left(name,2) order by (select 1)),1))          from CET1 where ltrim(rtrim(name)) not like '% %')select a.name,b.Up from #ta a,CET2 b where a.name = b.name/*name                                               Up-------------------------------------------------- --------customer code                                      CCproduct project                                    PPcustomer                                           CUproduct                                            PRproduct                                            PRproject                                            PO(6 行受影响)*/
------解决方案--------------------
探讨

好厉害呀!可是我没看懂你是怎么做到让product 和 project不重名的?
如果最后结果想要成CC_customer code 呢,也就是缩写加原名
引用:

SQL code
if object_id('tempdb..#ta')is not null drop table #ta
create table #ta(name varchar(50))
in……
  相关解决方案