April 21, 2019 at 8:41 pm
I am testing this:
SELECT LEN(TRIM(' SSMS '))
SELECT LEN(LTRIM(' SSMS '))
SELECT LEN(RTRIM(' SSMS '))
The results I am getting are:
4
4
9
For the LTRIM function, I expected to get 9, not 4. With LTRIM seems like the spaces to the right are ignored. If that is the case, then it does the same thing as TRIM. If that is the case then why would SQL even have a TRIM function?
What is happening here with the LTRIM function? After truncating the leading spaces, it should count SSMS plus the 5 spaces that follow, resulting in 9 for the length.
On a similar note, the following isn't working either:
SELECT LEN(LTRIM('.' FROM '.....SSMS.....'))
So it seems like there is something unusual about how the LTRIM function is being used, but most likely I am overlooking something.
April 22, 2019 at 1:25 am
Try using DATALENGTH instead:
SELECT DATALENGTH(TRIM(' SSMS '))
SELECT DATALENGTH(LTRIM(' SSMS '))
SELECT DATALENGTH(RTRIM(' SSMS '))
It still isn't likely to behave the way you are assuming with trailing spaces. Refer to this article for more information:
The other query that isn't working shouldn't be working. It's not valid and should error, not sure what you are trying to do there.
Sue
April 22, 2019 at 3:09 am
To add to what Sue has stated, LEN doesn't include trailing spaces in its count. Here's the note from Books Online. The emphasis is mine.
Returns the number of characters of the specified string expression, excluding trailing blanks.
Just a bit of a suggestion... never assume that you know what a function will actually do or return just by its name. Lookup the function and make sure especially when something unexpected occurs like with the problem you posed.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 22, 2019 at 4:07 am
Sue with the following code
SELECT LEN(LTRIM('.' FROM '.....SSMS.....'))
I was attempting to TRIM/remove all the '.' to the left of the letters SSMS, which would return: SSMS.....
The TRIM function can remove characters that are not spaces, so I figured that LTRIM could do the same but only from the left side. Can LTRIM and RTRIM remove specific characters?
April 22, 2019 at 6:22 am
Sue with the following code
SELECT LEN(LTRIM('.' FROM '.....SSMS.....'))I was attempting to TRIM/remove all the '.' to the left of the letters SSMS, which would return: SSMS..... The TRIM function can remove characters that are not spaces, so I figured that LTRIM could do the same but only from the left side. Can LTRIM and RTRIM remove specific characters?
TRIM can remove characters other than spaces, LTRIM and RTRIM an only remove spaces
😎
This works fine
SELECT LEN(TRIM('.' FROM '.....SSMS.....'))
April 22, 2019 at 11:49 am
TRIM can remove characters other than spaces, LTRIM and RTRIM an only remove spaces 😎 This works fine
SELECT LEN(TRIM('.' FROM '.....SSMS.....'))
Good lord... talk about a violation of the expected syntax for a function.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 22, 2019 at 12:24 pm
The newly introduced T-SQL TRIM() function is almost compatible with other vendor's flavors of SQL such as Oracle, PostgeSQL, Netezza etc.
😎
Of course it's not perfect as it is lacking the BOTH, LEADING and TRAILING directives, different number of characters can be passed. This means that during code migration, it is still on the incompatibility list 🙁
April 22, 2019 at 1:40 pm
I guess I don't understand the need for the parameters of BOTH, LEADING, and TRAILING. Apparently the industry standard has changed over time because, for me, TRIM() always meant both sides and if you wanted something else, you'd use either LTRIM() or RTRIM(). And having to use the word FROM in a function gives me the willies.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 22, 2019 at 2:01 pm
He he, a "standard" is these days a "temporal" thing, changes quicker than my girls mind in a shoe shop 😉
😎
IIRC, TRIM was in ANSII SQL 2003, maybe earlier but I can't remember.
April 23, 2019 at 2:44 am
TRIM can remove characters other than spaces, LTRIM and RTRIM an only remove spaces
So if I wanted to remove specific characters from the left or right, how could I do that since LTRIM and RTRIM won't work? Are there any T-SQL functions that can do that?
April 23, 2019 at 8:11 am
If you are talking about a contiguous leading non space character then a combination of PATINDEX and STUFF should work.
For trailing use REVERSE, PATINDEX and LEFT.
Far away is close at hand in the images of elsewhere.
Anon.
April 23, 2019 at 11:32 am
Here is a quick example
😎
USE TEEST;
GO
SET NOCOUNT ON;
DECLARE @TSTR VARCHAR(50) = '.....SSMS.....';
SELECT
STUFF(@TSTR,1,PATINDEX('%[^\.]%',@TSTR) - 1,'') AS TRIM_LEFT
,LEFT(@TSTR,(1 + DATALENGTH(@TSTR) - PATINDEX('%[^\.]%',REVERSE(@TSTR)))) AS TRIM_RIGHT
;
And the output
TRIM_LEFT TRIM_RIGHT
---------- -----------
SSMS..... .....SSMS
April 23, 2019 at 12:15 pm
Here is a quick example 😎
USE TEEST;
GO
SET NOCOUNT ON;
DECLARE @TSTR VARCHAR(50) = '.....SSMS.....';
SELECT
STUFF(@TSTR,1,PATINDEX('%[^\.]%',@TSTR) - 1,'') AS TRIM_LEFT
,LEFT(@TSTR,(1 + DATALENGTH(@TSTR) - PATINDEX('%[^\.]%',REVERSE(@TSTR)))) AS TRIM_RIGHT
;And the output TRIM_LEFT TRIM_RIGHT ---------- ----------- SSMS..... .....SSMS
Or
STUFF(@TSTR,1,PATINDEX('%[^\'+LEFT(@TSTR,1)+']%',@TSTR) - 1,'') AS TRIM_LEFT
,LEFT(@TSTR,(1 + DATALENGTH(@TSTR) - PATINDEX('%[^\'+RIGHT(@TSTR,1)+']%',REVERSE(@TSTR)))) AS TRIM_RIGHT
Far away is close at hand in the images of elsewhere.
Anon.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply