【问题标题】:How to trim all lowercase characters that comes before an uppercase character in a string using sql server如何使用sql server修剪字符串中大写字符之前的所有小写字符
【发布时间】:2019-07-02 06:21:50
【问题描述】:

如何编写 SQL Server 查询来修剪字符串中第一个大写字符之前的所有小写字符? 例如字符串值“eaplgCostPrice”,删除“eaplg”并将“CostPrice”传递到新列

【问题讨论】:

标签: sql sql-server


【解决方案1】:

如果PATINDEX 与某些排序规则结合使用,则它的功能取决于大小写。

示例 sn-p:

--
-- using a table variable for demonstration purposes
--
declare @Table table (id int identity(1,1), col varchar(30));
insert into @Table (col) values ('eaplgCostPrice'),('SellPrice'),('amount');

--
-- trim leading lowercases when needed
--
select col, 
(case
 when patindex('[a-z]%[a-z][A-Z]%', col COLLATE Latin1_General_Bin) > 0
 then substring(col, patindex('%[A-Z]%', col COLLATE Latin1_General_Bin), len(col))
 else col
 end) as trimmedCol
from @Table

结果:

col             trimmedCol
------------------  ----------------
eaplgCostPrice  CostPrice
SellPrice       SellPrice
amount          amount

【讨论】:

    猜你喜欢
    • 2015-02-20
    • 2017-08-02
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 2017-11-27
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多