首页 > 解决方案 > 将正确的函数应用于列中的子字符串

问题描述

我们的 SQL Server 2014 系统有一个propercase函数,效果很好。我正在尝试将其应用于包含出生城市、州和国家/地区组合的列的子字符串。

例如

birthplace
LOS ANGELES, CA US
DALLAS, TX US
GRAND RAPIDS, MI US

希望它是

birthplace
Los Angeles, CA US
Dallas, TX US
Grand Rapids, MI US

如果我应用它当前显示的propercase函数

birthplace
Los Angeles, Ca Us
Dallas, Tx Us
Grand Rapids, Mi Us

任何帮助,将不胜感激。

标签: sql-servertsql

解决方案


好 。. . 假设所有出生地都有一个逗号,并且您希望propercase()最多到第一个逗号:

select (dbo.propercase(left(birthplace, charindex(',', birthplace))) +
        substring(birthplace, charindex(',', birthplace) + 1)
       )

我会注意到这种方法不能修复拼写错误,因此它不会将“Grand Rapids”更改为“Grand Rapids”。;)


推荐阅读