SQL查询将数据更新为UPPER CASE仅第4个字?

时间:2017-10-18 09:18:27

标签: sql-server

我有一个类似&#34的数据; Java是一个程序1223语言"在行中,der在我的数据库中有数百个这样的数据,我只想使用sql查询的第四个单词的大写字母(在给定的句子中#34; Program123ming")。

我尝试过这个查询

UPDATE student 
SET stud_dept = UPPER(SUBSTRING(stud_dept, 0, 3))

但是由于SUBSTRING函数只显示了3个单词,我们还能用什么才能在大写字母中仅制作第4个单词?

3 个答案:

答案 0 :(得分:1)

以下是使用CHARINDEXSUBSTRING

的一种方法
SELECT UpperCase = LEFT(string, p3) + Upper(Substring(string, p3, p4-p3)) + Substring(string, p4, Len(string))
FROM   Yourtable
       CROSS apply (SELECT Charindex(' ', string)) fst (p1)
       CROSS apply (SELECT Charindex(' ', string, p1 + 1)) scd (p2)
       CROSS apply (SELECT Charindex(' ', string, p2 + 1)) thd (p3)
       CROSS apply (SELECT Charindex(' ', string, p3 + 1)) fou (p4) 

答案 1 :(得分:1)

您可以将附加的脚本包装到参数化( @var )函数中,它可以解决这个问题:

DECLARE @var NVARCHAR(max) = N'Java is a Program123ming a Language';

IF LEN(@var) - LEN(REPLACE(@var,' ','')) >= 3
BEGIN   
    DECLARE @firstSpaceIndex INT = CHARINDEX(' ',@var)
    DECLARE @secondSpaceIndex INT = CHARINDEX(' ',@var,@firstSpaceIndex + 1)
    DECLARE @thirdSpaceIndex INT = CHARINDEX(' ',@var,@secondSpaceIndex + 1)
    DECLARE @fourthSpaceIndex INT = CHARINDEX(' ',@var,@thirdSpaceIndex + 1)

    IF @fourthSpaceIndex = 0
    BEGIN 
        SET @fourthSpaceIndex = LEN(@var);
    END 

    SET @var =
    SUBSTRING(@var,1,@firstSpaceIndex) + 
    SUBSTRING(@var,@firstSpaceIndex + 1,@secondSpaceIndex - @firstSpaceIndex) + 
    SUBSTRING(@var,@secondSpaceIndex + 1, @thirdSpaceIndex - @secondSpaceIndex ) + 
    UPPER(SUBSTRING(@var,@thirdSpaceIndex + 1,@fourthSpaceIndex - @thirdSpaceIndex) )+
    SUBSTRING(@var,@fourthSpaceIndex + 1,LEN(@var) - @fourthSpaceIndex) 
END 

SELECT @var;

<强> - 更新 这是包装代码,只需使用您的表并将目标列传递给函数:

CREATE FUNCTION dbo.Upper4thWord(
@var NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
BEGIN

    IF LEN(@var) - LEN(REPLACE(@var,' ','')) >= 3
    BEGIN   
        DECLARE @firstSpaceIndex INT = CHARINDEX(' ',@var)
        DECLARE @secondSpaceIndex INT = CHARINDEX(' ',@var,@firstSpaceIndex + 1)
        DECLARE @thirdSpaceIndex INT = CHARINDEX(' ',@var,@secondSpaceIndex + 1)
        DECLARE @fourthSpaceIndex INT = CHARINDEX(' ',@var,@thirdSpaceIndex + 1)

        IF @fourthSpaceIndex = 0
        BEGIN 
            SET @fourthSpaceIndex = LEN(@var);
        END 

        SET @var =
        SUBSTRING(@var,1,@firstSpaceIndex) + 
        SUBSTRING(@var,@firstSpaceIndex + 1,@secondSpaceIndex - @firstSpaceIndex) + 
        SUBSTRING(@var,@secondSpaceIndex + 1, @thirdSpaceIndex - @secondSpaceIndex ) + 
        UPPER(SUBSTRING(@var,@thirdSpaceIndex + 1,@fourthSpaceIndex - @thirdSpaceIndex) )+
        SUBSTRING(@var,@fourthSpaceIndex + 1,LEN(@var) - @fourthSpaceIndex) 
    END 

    RETURN @var;
END 
GO
DECLARE @var NVARCHAR(max) = N'Java is a Program123ming a Language';
SELECT dbo.Upper4thWord(@var)

答案 2 :(得分:1)

看看我使用CTE的解决方案。我认为最好使用PATINDEX代替CHARINDEX来查找字符串中的单词。

DECLARE @Test   TABLE (
    stud_dept VARCHAR(8000)
)

INSERT @Test
VALUES
    ('Java is a Program123ming a Language'),
    ('Java is   good '),
    ('Java is   good idea'),
    ('Java is a Program123ming'),
    ('Java is a Program123ming a LanguageJava is a Program123ming a Language')

UPDATE @Test
SET stud_dept = RTRIM (stud_dept)

;with student
AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY stud_dept) AS ID, stud_dept
    FROM @Test
),

cte
AS
(
    SELECT ID, LEFT(stud_dept, PATINDEX('%[^ ] %', stud_dept) + 1) AS s1, 1 AS Number, RIGHT(stud_dept, LEN(stud_dept) - (PATINDEX('%[^ ] %', stud_dept) + 1)) AS s2
    FROM student

    UNION ALL

    SELECT s.ID, s1 + CASE WHEN Number = 3 
        THEN UPPER(LEFT(s2, PATINDEX('%[^ ] %', s2) + 1))
        ELSE LEFT(s2, PATINDEX('%[^ ] %', s2) + 1)
        END  AS s1, Number + 1, 
        RIGHT(s2, LEN(s2) - (PATINDEX('%[^ ] %', s2) + 1)) AS s2
    FROM cte
        JOIN student s ON s.ID = cte.ID
    WHERE Number < 4
        AND PATINDEX('%[^ ] %', s2) > 0
)

UPDATE s
SET stud_dept = s1 + CASE WHEN m.Number = 3 THEN UPPER(s2) ELSE s2 END
FROM cte c
    JOIN student s ON c.ID = s.ID
    CROSS APPLY (SELECT MAX(Number) AS Number FROM cte WHERE ID = s.ID) m
WHERE m.Number = c.Number

SELECT * FROM @Test