Remove last number with sql

Asked

Viewed 7,703 times

2

I would like to take the last paragraph 1 from this sequence:

0495747500000049908275289000100040000000041

How do I do in sql server?

  • Will always remove the last character or will remove the last one only be for 1?

  • not any number

  • Is the size fixed? 43 characters?

  • 1

    To take, to extract, to obtain, I think would change the perception of the question presented somewhat. But, for what you want, the LEFT, RIGHT and SUBSTRING functions work very well

3 answers

7

Can combine the function left() to pick up all characters starting left until the limit 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)
  • what if it’s a variable? type @test =

  • It worked. Thank you

  • @Felipemichaeldafonseca if any answer solved, mark as chosen by clicking on the green V next to the points

3

You can use both LEFT and 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 string with the number of specified characters.

SUBSTRING (Transact-SQL)

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

Sqlfiddle

2

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

SELECT RIGHT(CAMPO, 1)

If your field is a number you need to convert to varchar:

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

Don’t forget to change the size of the Varchar for your need.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.