简体   繁体   中英

Replace function combined with ltrim and rtrim

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 ' ' ?

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).

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 ... ie 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.

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:

  1. remove all trailing and leading spaces: LTRIM(RTRIM(CompletionDetails))
  2. replace 3 with comma: REPLACE( ?, '3', ',')
  3. replace it with ' ': REPLACE(? , '.', ' ') or REPLACE(? , '.', '')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM