Not able to convert numeric to String in postgres sql - postgresql

I am not able to convert numeric to string. For example below is the query:
select to_char(50000.00,'999D99S')
Any help would be thankful.

You have to add more 9:
select to_char(50000.00,'99 999D99S');
If you don't know how much decimal you can have, just do something like:
select to_char(50000.00,'999 999 999 999D99S');

Below syntax helped me:
select 50000.00::text as content

Related

How to extract numbers appearing after decimal from a money format column in tsql?

I have an amount column which is in money format. I have tried using parsename, converting it to varchar to use substring function but unable to extract exact values appearing after decimal. Attaching the screenshot for reference.
select home_currency_amount,
cast(home_currency_amount as varchar(50)) as amt_varchar,
parsename(home_currency_amount, 1) as amt_prsnm
from #temptbl;
---Below is the output:
home_currency_amount amt_varchar amt_prsnm
39396.855 39396.86 86
1112.465 1112.47 47
5635.1824 5635.18 18
E.g. if value is 39396.855, desired output would be 855.
Thanks in advance for the help.
First perform the mod operation on value with 1 to get the decimal part. We will be getting as '0.decimal_part' as we require only decimal part without '0.' so we are replacing it and finally casting as integer. Hope it helps in your case..
select cast(replace(cast( 1.23 % 1 as varchar),'0.','') as int)
enter image description here

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 everything after certain character in SQL?

I've got a list 400 rows +. Each row looks similar to this: example-example123 I would like to remove everything past '-' so that I'm left with just the beginning part: example123
Any help would be greatly appreciated.
try it like this:
UPDATE table SET column_name=LEFT(column_name, INSTR(column_name, '-')-1)
WHERE INSTR(column_name, '-')>0;
If you only want to select you do it this way:
SELECT LEFT(column_name, INSTR(column_name, '-')-1) FROM table;
INSTR function gets you the position of your - then you update the column value to become from the first letter of the string till the position of the - -1
Here's a fiddle
You can use SQL Trim() function
SELECT TRIM(TRAILING '-' FROM BHEXLIVESQLVS1-LIVE61MSSQL)
AS TRAILING_TRIM
FROM table;
The result should be "BHEXLIVESQLVS1"
select SUBSTRING(col_name,0,Charindex ('-',col_name))
Assuming you need to do this in a query, you can use the string functions of your database.
For DB2 this would look something like
select SUBSTR(YOURCOLUMN, 1, LOCATE('-',YOURCOLUMN)) from YOURTABLE where ...
In SQL Server you could use
SUBSTRING
and
CHARINDEX
For SQL server you can do this,
LEFT(columnName, charindex('-', columnName)) to remove every character after '-'
to remove the special character as well do this,
LEFT(columnName, charindex('-', columnName)-1)
SELECT SUBSTRING(col_name,0,Charindex ('-',col_name)) FROM table_name
WHERE col_name='yourvalue'
Eg.
SELECT SUBSTRING(TPBS_Path,0,Charindex ('->',TPBS_Path)) FROM [CFG].[CFG_T_Project_Breakdown_Structure] WHERE TPBS_Parent_PBS_Code='LE180404'
here TPBS_Path is the column for which trim is to be done and [CFG].[CFG_T_Project_Breakdown_Structure] is table name and TPBS_Parent_PBS_Code='LE180404' is the select condition. Everything after '->' will be trimmed

Get substring form an integer column

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.

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.