4

I had a quick question, how can I go about using SUBSTRING on an integer? I currently have field labeled "StoreID" that contains a 5 digit integer (60008). I am trying to use SUBSTRING to remove the 6 when I query out this information. When I use something like:

SUBSTRING('StoreID', 2, 6) I get an error returning back saying that SUBSTRING(integer,integer,integer) does not exist.

Is there another function I can use in postgres to accomplish what I am trying to do?

2 Answers 2

8

You can cast the integer

SUBSTR(cast (StoreId as text), 2,6)
Sign up to request clarification or add additional context in comments.

Comments

5

If you are interested in the number 8, use the modulo operator %

SELECT 60008%10000

If you want the string '0008', the function right() is right for you (added with Postgres 9.1):

SELECT right(60008::text, -1)

Or with modulo and to_char():

SELECT to_char(60008%10000, 'FM0000')

The FM modifier removes space padding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.