如果Rick Smith-hopkins如何在破折号后将角色大写? 看起来像这样:
Rick Smith-Hopkins
我的查询:
SELECT
LEFT([Person], 1) + LOWER(SUBSTRING([Person], 2, CHARINDEX(' ', [Person] + ' ') - 2)) 'name'
,LEFT(SUBSTRING([Person], CHARINDEX(' ', [Person]) + 1, LEN([Person])), 1) + LOWER(SUBSTRING([Person], CHARINDEX(' ', [Person]) + 2, LEN([Person]))) 'full surname'
,LEFT(SUBSTRING([Person], CHARINDEX(' ', [Person]) + 1, LEN([Person])), 1)
+ LOWER(SUBSTRING([Person], CHARINDEX(' ', [Person]) + 2, LEN([Person]))) 'surname 1st with -'
,LEFT([Person], 1) + LOWER(SUBSTRING([Person], 2, CHARINDEX(' ', [Person] + ' ') - 2)) + ' ' +
LEFT(SUBSTRING([Person], CHARINDEX(' ', [Person]) + 1, LEN([Person])), 1)
+ LOWER(SUBSTRING([Person], CHARINDEX(' ', [Person]) + 2, LEN([Person]))) 'Person'
FROM [db].[dbo].[table]
答案 0 :(得分:1)
我认为你需要一些scaller函数,它将char( - ),string_values作为参数。但是让我尝试使用stuff()
函数来处理这个问题,以替换为上部字符
SELECT
stuff(name, charindex('-', name)+1, 1,
upper(substring(name, charindex('-', name)+1, 1))) name,
....
FROM table
注意:这仅限于一个char( - )位置。
答案 1 :(得分:0)
根据Ivan的建议,您应该只在客户端处理此问题。
如果需要在数据库级别执行此操作,请按如下方式创建函数:
CREATE FUNCTION [dbo].[fnConvert_TitleCase] (@InputString VARCHAR(4000) )
RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE @Index INT
DECLARE @Char CHAR(1)
DECLARE @OutputString VARCHAR(255)
SET @OutputString = LOWER(@InputString)
SET @Index = 2
SET @OutputString = STUFF(@OutputString, 1, 1,UPPER(SUBSTRING(@InputString,1,1)))
WHILE @Index <= LEN(@InputString)
BEGIN
SET @Char = SUBSTRING(@InputString, @Index, 1)
IF @Char IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&','''','(')
IF @Index + 1 <= LEN(@InputString)
BEGIN
IF @Char != ''''
OR
UPPER(SUBSTRING(@InputString, @Index + 1, 1)) != 'S'
SET @OutputString =
STUFF(@OutputString, @Index + 1, 1,UPPER(SUBSTRING(@InputString, @Index + 1, 1)))
END
SET @Index = @Index + 1
END
RETURN ISNULL(@OutputString,'')
END