How to remove the first character of a string in a Query

1

In PostgreSQL 9.2 I have a column with the following information:

A101
B12
C3

I need a command to select and another one to update that column by removing the first character by doing so:

101
12
3

I tried to solve with the substring function, but I could not get the expected result.

How to solve?

    
asked by anonymous 01.09.2017 / 22:32

1 answer

2

As you passed your column from which you do not want the first character, then let's consider any and all character types.

To select the data by removing the first character, and using SUBSTR , we can do the following:

SELECT SUBSTR(info,2,LENGTH(info)) 
  FROM INFORMACAO 

Example: link

  

Substr function:
substr(string, from [, count])
  The first parameter being the text, the second the beginning of the count of   characters of the text, and the third the total of characters.

In this way, we then have to pick up from the second character, up to the total size of the text, we get this through the function lenght , which calculates the total characters in a text.

Now to update to the column, we use the same functions as above:

UPDATE informacao 
   SET info = SUBSTR(info,2, LENGTH(INFO));

There are other ways to do what you expect, but I did using the substr of which was quoted in the question, for more learning.

    
05.09.2017 / 03:36