开发者

T-SQL trim not working - let spaces on the result

开发者 https://www.devze.com 2023-03-13 16:31 出处:网络
I have a trim function that apply ltrim and rtrim CREATE FUNCTION dbo.TRIM(@string VARCHAR(MAX)) RETURNS VARCHAR(MAX)

I have a trim function that apply ltrim and rtrim

CREATE FUNCTION dbo.TRIM(@string VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(@string))
END
GO

I do the following query:

SELECT distinct dbo.trim([subject]) as subject
FROM [DISTR]

The result has rows开发者_开发技巧 like:

"A"
"A  "
"B"
...

I thought that thoose chars maybe weren't spaces but when I got the ascii code, it returns 32 which is the code for space.

My only guess is that I had to change the collaction of the database to: SQL_Latin1_General_CP1_CI_AI

Can that be the problem? Any ideas? Thanks


Maybe your field contains more than spaces. Remember than " " could be a space, tab, and many other "blank" chars. It's possible to match it using ASCII or building a CLR implementation of trim that uses regular expressions

0

精彩评论

暂无评论...
验证码 换一张
取 消