trim values from left using SQL

2019-08-17 19:59发布

问题:

I want to trim the characters from the left in my SQL value:

I have the following value:

ABC0005953

How do i trim the value 3 characters from the left? I would like to see:

005953

Edit my value is:

SELECT LEN(TABLE.VALUE)-3)

回答1:

SELECT SUBSTRING('ABC0005953', 4, LEN('ABC0005953'))

Start at the fourth character and keep going.

(Just posting as an alternative to the RIGHT(...) solution.)

In response to your update, I assume you mean you want to apply the above to your table:

SELECT SUBSTRING(TABLE.VALUE, 4, LEN(TABLE.VALUE))
FROM TABLE

From your other question:

I have the following:

SELECT DISTINCT

Left(GIFTHEADER.pID + GIFTHEADER.PID + '-' + Cast(PAYMENTDETAIL.PLINENO as Varchar),18)

AS TRANSACTIONREF...

Currently my value looks like this:

ABC0005953ABC0005953

I want to simply strip off the first 4 characters from GIFTHEADER.pID

If you want to remove the first four characters from GIFTHEADER.pID, I would recommend removing them before putting the value into your combined string:

SELECT DISTINCT
    LEFT(SUBSTRING(GIFTHEADER.pID, 5, LEN(GIFTHEADER.pID) +
        GIFTHEADER.PID +
        '-' +
        Cast(PAYMENTDETAIL.PLINENO as Varchar),18)
    AS TRANSACTIONREF


回答2:

You can use STUFF function to replace chars 1 thru 3 with an empty string

SELECT STUFF('ABC0005953',1,3,'')

I believe it's a better and more universal approach than cutting string.



回答3:

SELECT RIGHT('ABC0005953', LEN('ABC0005953') - 3)


回答4:

Perhaps you could could just use right(x, len(x)-3)



回答5:

Two options for you:

SELECT SUBSTRING('ABC0005953', 5,7)

SELECT RIGHT('ABC0005953', (LEN('ABC0005953') - 4))