Trimming text strings in SQL Server 2008

后端 未结 7 1755
无人共我
无人共我 2020-12-30 19:05

I have a table in a SQL Server 2008 database. This table has a nvarchar(256) column called \'Name\'. Unfortunately, the values in this field have extra spaces included. For

7条回答
  •  佛祖请我去吃肉
    2020-12-30 19:26

    This function trims a string from left and right. Also it removes carriage returns from the string, an action which is not done by the LTRIM and RTRIM

    IF OBJECT_ID(N'dbo.TRIM', N'FN') IS NOT NULL
        DROP FUNCTION dbo.TRIM;
    GO
    CREATE FUNCTION dbo.TRIM (@STR NVARCHAR(MAX)) RETURNS NVARCHAR(MAX)
    BEGIN
        RETURN(LTRIM(RTRIM(REPLACE(REPLACE(@STR ,CHAR(10),''),CHAR(13),''))))
    END;
    GO
    

提交回复
热议问题