Remove last number with sql

1

I would like to remove the last number or 1 in this sequence:

0495747500000049908275289000100040000000041

How do I do in sql server?

    
asked by anonymous 04.09.2018 / 22:42

3 answers

6

You can combine the left() function to get all characters starting from the left to the limit that is set by len() (which returns the string size) -1 . Something like:

select left('0495747500000049908275289000100040000000041',
len('0495747500000049908275289000100040000000041') - 1)

Or

select left(campo, len(campo) - 1)
    
04.09.2018 / 22:48
2

You can use the RIGHT function, it would look like this:

SELECT RIGHT(CAMPO, 1)

If your field is a number need convert to varchar:

SELECT RIGHT(CAST(CAMPO AS VARCHAR(50)))

Do not forget to change the size of Varchar to your requirement.

I hope I have helped.

    
04.09.2018 / 22:54
2

You can use either LEFT or SUBSTRING.

DECLARE @numero VARCHAR(MAX)

SET @numero = '0495747500000049908275289000100040000000041'

SELECT LEFT(@numero, LEN(@numero) - 1) AS 'LEFT', SUBSTRING(@numero, 0, LEN(@numero)) AS 'SUBSTRING';

LEFT (Transact-SQL)

  

Returns the left part of a character string with the number   of specified characters.

SUBSTRING (Transact-SQL)

  

Returns part of a character, binary, text, or   image in SQL Server.

SqlFiddle

    
04.09.2018 / 23:58