如何在T-SQL中将CamelCase转换为带有空格的字符串

时间:2018-12-18 04:04:47

标签: sql sql-server tsql

如何基于T-SQL中的适当大小写将“ ThisIsTest”之类的字符串转换为“ This Is Test”?

  • 输入:“ ColumnA”
  • 输出:“ A列”

1 个答案:

答案 0 :(得分:1)

尝试一下。

DECLARE @Input VARCHAR(MAX)='ColumnA'

DECLARE @Regex AS VArCHAR(MAX), @Output AS VARCHAR(MAX)    

SET @Output = @Input
SET @Regex = '%[^ ][A-Z]%'

WHILE PATINDEX(@Regex collate Latin1_General_Bin, @Output) > 0
BEGIN
    SET @Output = Stuff(@Output, PatIndex(@Regex collate Latin1_General_Bin, @Output) + 1, 0, ' ')
END

SELECT @Output AS [Output]