to_char(number) function in postgres - postgresql

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.

Related

Change currency sign oracle d2k reports?

I want to replace $ sign to 'Rs.' in oracle d2k reports. In some system it is displaying Rs but in some system it is showing $. From where I have to change the sign.
You can use the currency in your NLS_TERRITORY settings as follows:
select to_char(123456789.91, 'L999,999,999,990.00') from dual;
L999,999,999,990.00 is the format mask you may be able to set in the property sheet (it's a while since I used Reports) or you can use a sql function like in the example above.
Or you can take the date and format it as a string (as above) and concatenate with the character you want to display. Obviously this isn't as flexible.
select 'Rs'||to_char(123456789.91, '999,999,999,990.00') from dual;
You can check your nls_settings by connecting in sqlplus
SELECT * FROM nls_session_parameters;
You can use this code as well.
SELECT TO_CHAR
(-10000,
'L99G999D99MI',
'NLS_NUMERIC_CHARACTERS = '',.''
NLS_CURRENCY = ''RS'' '
) "Amount"
FROM DUAL;

PostgreSQL Trim excessive trailing zeroes: type numeric but expression is of type text

I'm trying to clean out excessive trailing zeros, I used the following query...
UPDATE _table_ SET _column_=trim(trailing '00' FROM '_column_');
...and I received the following error:
ERROR: column "_column_" is of
expression is of type text.
I've played around with the quotes since that usually is what it barrels down to for text versus numeric though without any luck.
The CREATE TABLE syntax:
CREATE TABLE _table_ (
id bigint NOT NULL,
x bigint,
y bigint,
_column_ numeric
);
You can cast the arguments from and the result back to numeric:
UPDATE _table_ SET _column_=trim(trailing '00' FROM _column_::text)::numeric;
Also note that you don't quote column names with single quotes as you did.
Postgres version 13 now comes with the trim_scale() function:
UPDATE _table_ SET _column_ = trim_scale(_column_);
trim takes string parameters, so _column_ has to be cast to a string (varchar for example). Then, the result of trim has to be cast back to numeric.
UPDATE _table_ SET _column_=trim(trailing '00' FROM _column_::varchar)::numeric;
Another (arguably more consistent) way to clean out the trailing zeroes from a NUMERIC field would be to use something like the following:
UPDATE _table_ SET _column_ = CAST(to_char(_column_, 'FM999999999990.999999') AS NUMERIC);
Note that you would have to modify the FM pattern to match the maximum expected precision and scale of your _column_ field. For more details on the FM pattern modifier and/or the to_char(..) function see the PostgreSQL docs here and here.
Edit: Also, see the following post on the gnumed-devel mailing list for a longer and more thorough explanation on this approach.
Be careful with all the answers here. Although this looks like a simple problem, it's not.
If you have pg 13 or higher, you should use trim_scale (there is an answer about that already). If not, here is my "Polyfill":
DO $x$
BEGIN
IF count(*)=0 FROM pg_proc where proname='trim_scale' THEN
CREATE FUNCTION trim_scale(numeric) RETURNS numeric AS $$
SELECT CASE WHEN trim($1::text, '0')::numeric = $1 THEN trim($1::text, '0')::numeric ELSE $1 END $$
LANGUAGE SQL;
END IF;
END;
$x$;
And here is a query for testing the answers:
WITH test as (SELECT unnest(string_to_array('1|2.0|0030.00|4.123456000|300000','|'))::numeric _column_)
SELECT _column_ original,
trim(trailing '00' FROM _column_::text)::numeric accepted_answer,
CAST(to_char(_column_, 'FM999999999990.999') AS NUMERIC) another_fancy_one,
CASE WHEN trim(_column_::text, '0')::numeric = _column_ THEN trim(_column_::text, '0')::numeric ELSE _column_ END my FROM test;
Well... it looks like, I'm trying to show the flaws of the earlier answers, while just can't come up with other testcases. Maybe you should write more, if you can.
I'm like short syntax instead of fancy sql keywords, so I always go with :: over CAST and function call with comma separated args over constructs like trim(trailing '00' FROM _column_). But it's a personal taste only, you should check your company or team standards (and fight for change them XD)

Not able to convert numeric to String in postgres sql

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

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.