Reputation: 695
Can someone please help me to understand the following code:
REPLACE(LTRIM(RTRIM(dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ','))), '.', '') AS BuildRequestID,
Does it say remove all trailing and leading spaces, then replace 3 with comma. Next, if there is .
, replace it with ' '
?
Upvotes: 1
Views: 7308
Reputation: 416049
It does not at any point replace 3
with ,
.
We can make all this easier to follow by formatting the full expression to cover multiple lines:
REPLACE(
LTRIM(RTRIM(
dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ',')
))
,'.', ''
) AS BuildRequestID,
Expressions like this have to read from the inside out. So we start with this inner-most part:
dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ',')
This UFN_SEPARATES_COLUMNS()
function is not part of Sql Server, but was added by someone at your organization or as part of the vendor software package for the database you're looking at. But I'm confident based on inferences and the link (found via Google) it will treat CompletionDetails
as delimited text, where the delimiter is a comma (based on the 3rd ','
argument) and returns the 3rd field (based on the 2nd 3
argument, where counting starts at 1 rather than 0). As CSV parsers go, this one is particularly naive, so be very careful what you expect from it.
Then we use LTRIM()
and RTRIM()
to remove both leading and trailing blanks from the field. Not all whitepsace is removed; only space characters. Tabs, line feeds, etc are not trimmed. Sql Server 2017 has a new TRIM()
function that can handle wider character sets and do both sides of the string with one call.
The code then uses the REPLACE()
function to remove all .
characters from the result (replaces them with an empty string).
Upvotes: 2
Reputation: 164139
remove all trailing and leading spaces, then replace 3 with comma. Next, if there is ., replace it with ' '
No it does not say that.
But this does:
REPLACE(REPLACE(LTRIM(RTRIM(CompletionDetails)), '3', ','), '.', ' ')
it's not clear if you want .
replaced by ' '
or ''
.
I used the 1st case, you can change it as you like.
It's easier to understand like this:
LTRIM(RTRIM(CompletionDetails))
REPLACE( ?, '3', ',')
REPLACE(? , '.', ' ')
or REPLACE(? , '.', '')
Upvotes: 0
Reputation: 1051
The LTRIM removes leading spaces. RTRIM removes trailing spaces. REPLACE removes the period.
Declare @Val Char(20) = ' Frid.ay '
Select REPLACE(
LTRIM(
RTRIM(
@Val --dbo.x_COLUMNS(CompletionDetails, 3, ',')
)
), '.', ''
)
Result
BuildRequestID
--------------
Friday
Upvotes: 0
Reputation: 25132
The code is trimming the leading and trailing spaces via the LTRIM()
and RTRIM()
functions of whatever is returned from the function dbo.x_COLUMNS
... i.e. dbo.x_COLUMNS(CompletionDetails, 3, ',')
. LTRIM
is left, RTRIM
is right.
It then is replacing all periods (.
) with nothing via the REPLACE()
function.
So in summary, it's removing all periods from the string and the leading and trailing spaces.
Upvotes: 0