Get substring form an integer column - postgresql

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?

You can cast the integer
SUBSTR(cast (StoreId as text), 2,6)

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.

Related

DB2: Need to extract string to the left of delimitere

I have a column that looks like this:
SBN:123456=1
SBN:1234=0
SBN:12345678=5
I need to extract everything left of the equal sign ('=') for every row. I attempted using SUBSTRING this way:
SELECT COLUMN1, SUBSTR(COLUMN2,1,LOCATE('=', COLUMN2)-1) AS STUFF FROM TABLE1;
Instead of extracting the text from the string, it gave me the error "The statement was not executed because a numeric argument of a scalar function is out of range." and I can't seem to figure out why. What am I doing wrong?
I'm using DB2 11.1.4.4 on AIX, just FYI.
I found the issue. There were some NULLs in the column that the query didn't like apparently. Got rid of those and it worked fine.

how to remove first char with to_char or another function in postgresql?

I have the follow function in a SQL query:
to_char(currval('admatencionc."TMReporte_ID_reporte_seq"'::regclass),'FM0000')
It takes the sequence and converted to a char filled with 0, but recently the sequence reached the 10000 place. I am looking a way to supress the first number to have again the 0000 number.
I am looking the documentation of to_char but I don't find anything.
As you actually appear to want the last 4 digits, rather than to remove the first char, use right:
SELECT right(to_char(..., ...), 4);
IMO it's cleaner to mathematically get the right value, then convert it to a string, though.
SELECT to_char(currval('blah') % 10000, 'FM0000');

Postgresql, treat text as numbers for getting MAX function result

Still didnt fix issue with dates written as strings here comes another problem.
I have text column where only numbers as writen (like text).
By using function MAX I get incorrect result because there 9 is bigger than 30.
Is here any inline function like VAL or CINT or something that I can compare and use textual data (only numbers) like numbers in queries like SELECT, MAX and other similar?
How than can look like in following examples:
mCmd = New OdbcCommand("SELECT MAX(myTextColumn) FROM " & myTable, mCon)
You need to use max(to_number(myTextColumn, '999999'))
More details are in the manual: http://www.postgresql.org/docs/current/static/functions-formatting.html
If all "numbers" are integers, you can also use the cast operator: max(myTextColumn::int)
If your text values are properly formatted you can simply cast them to double, e.g.: '3.14'::numeric.
If the text is not formatted according to the language settings you need to use to_number() with a format mask containing the decimal separator: to_number('3.14', '9.99')
To get the MAX works poterly you need to first convert your text field in numeric format
mCmd = New OdbcCommand("SELECT MAX(TO_NUMBER(myTextColumn, '99999')) FROM " & myTable, mCon)

to_char(number) function in postgres

i want to display/convert a number to character (of it's same length) using to_char() function .
In oracle i can write like
SELECT to_char(1234) FROM DUAL
But in postgres
SELECT to_char(1234)
is not working.
You need to supply a format mask. In PostgreSQL there is no default:
select to_char(1234, 'FM9999');
If you don't know how many digits there are, just estimate the maximum:
select to_char(1234, 'FM999999999999999999');
If the number has less digits, this won't have any side effects.
If you don't need any formatting (like decimal point, thousands separator) you can also simply cast the value to text:
select 1234::text
you have to specify a numeric format, ie:
to_char(1234, '9999')
Take a look here for more info: http://www.postgresql.org/docs/current/static/functions-formatting.html
CAST function worked for me.
SELECT CAST(integerv AS text) AS textv
FROM (
SELECT 1234 AS integerv
) x
OR
SELECT integerv::text AS textv
FROM (
SELECT 1234 AS integerv
) x
You can use:
1234||''
It works on most databases.

How to get number digit from the varchar 2 in oracle?

I have variable l_string varchar 2(100).
The value of string is l_string='L000EW45UY678IOP'.
I want to get number from this string like
00045678
Please tell me how to get the above string result.
Google the TRANSLATE function.
It will let you replace the alphabetic characters by nothing, leaving the digits behind.
try this:
regexp_replace('L000EW45UY678IOP', '[^0-9]', '')