Number format equivalent function in PostgreSQL - postgresql

Trying to format the number as per the given format and culture.
Given:
-4059587.225000, --Value
'#,##0.00;(#,##0.00)' --Format
'en-US' --Culture
Will have many patterns, the given one is for example.
Expected output: (4,059,587.23)
In SQL Server we have format() function, what's the equivalent in PostgreSQL?
My try:
select to_char( -4059587.225000, '#,##0.00;(#,##0.00)' );
Error:
multiple decimal points

Use to_char:
SET lc_numeric = 'en_US';
SELECT translate(to_char(-4059587.225000, '9G999G999D99PRFM'), '<>', '()');
translate
════════════════
(4,059,587.23)
(1 row)
The documentation describes the available formats.

How about:
select
concat( to_char( -4059587.225000, '#,##0.00;' ), to_char( -4059587.225000, '(#,##0.00)' ) );

Related

Convert Informatica Functions into Postgres

I have a workflows in the Informatica which has Expression RTRIM(LTRIM(EMP_NUM,'0')). This function I want to convert it in to the Postgres query. How can we do that ?
SELECT EMP_NUM, EMP_NAME, EMP_EMAIL FROM TEST.EMPLOYEE
Any pointers ?
Sample Data of EMP_NUM
01000
11
0000176
00090900088
08009345353
To replicate what you show:
select rtrim(ltrim('001000 ', ' 0'));
rtrim
-------
1000
(1 row)
String operators and functions for Postgres:
https://www.postgresql.org/docs/current/functions-string.html
An alternate solution(converts to numeric):
select to_number('01000 ', '99999');
to_number
-----------
1000
(1 row)
https://www.postgresql.org/docs/current/functions-formatting.html
to_number(text, text) numeric convert string to numeric to_number('12,454.8-', '99G999D9S')

SQL Server : Convert from VARCHAR to DATETIME

Current Date Format: 12-18-2018-03:14:48
I want to convert to: 2018-12-18 03:14
Currently using SQL Server 2008
I'm using this code syntax:
DECLARE #input VARCHAR(35) = '12-18-2018-03:14:48'
SELECT CONVERT(DATETIME, #input, 120)
Error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Please help. Thank you!
I'm not sure that your current datetime literal falls into any mask which SQL Server can recognize. But we can try using TRY_CONVERT here, replacing the middle dash with a space:
SELECT TRY_CONVERT(datetime, STUFF(#input, 11, 1, ' ')) AS output;
18/12/2018 03:14:48
Demo
Edit:
If you are using an earlier version of SQL Server which does not support TRY_CONVERT, then we can try explicitly using CONVERT:
SELECT CONVERT(datetime, STUFF(#input, 11, 1, ' ')) AS output;

How to convert a time from text to time format in postgresQL?

I have done this query in PostgreSQL:
select b.movement_category,
round(count(*)*100/case when count(*) = 0 then 1 else sum(count(*)) over () end::numeric,2) cnt_pct
from (
select distinct a.imei_number, a.movement_category
from dataview a
where a.data_capture_time between '2018-07-28' and '2018-07-28'
and a.movement_category <> ''
and fn_distance_km(23.728264,90.411367,cast(a.latitude as double precision),cast(a.longitude as double precision))<=20
) b
group by b.movement_category
Take a look at the documentation for formatting functions for converting various data types.
One example for your case could be:
SELECT to_timestamp('05 Dec 2000 15:15:20', 'DD Mon YYYY HH24:MI:SS')::TIME
which will return 15:15:20

getting formatted column value in postgres

I have a column in my table having values of format:
COURSE_214/MODULE_5825/SUBMODULE_123/..../GOAL_124/ACTIVITY_125.
I need value for goal i.e 124 from goal_124. I am planning to use 'regexp_split_to_array' but don't know how to use elements from array.
I am using postgres 9.2.
You can use split_part like so:
select split_part(split_part('COURSE_214/MODULE_5825/SUBMODULE_123/..../GOAL_124/ACTIVITY_125', '/GOAL_', 2), '/', 1)
i.e.
select split_part(split_part(fieldname, '/GOAL_', 2), '/', 1)
Result:
124
Using json_object():
select json_object(string_to_array(translate(params, '_', '/'), '/'))
from test
json_object
------------------------------------------------------------------------------------------------
{"COURSE" : "214", "MODULE" : "5825", "SUBMODULE" : "123", "GOAL" : "124", "ACTIVITY" : "125"}
(1 row)
select json_object(string_to_array(translate(params, '_', '/'), '/'))->>'GOAL' as goal
from test
goal
------
124
(1 row)
The column has a format suitable for json. I would suggest to change the type of the column to jsonb. The first query may be used as a converter.
After the conversion you would access the parameters in an easy way, e.g.:
select *
from test
where params->>'COURSE' = '214'
and (params->>'GOAL')::int > 120;
Simple select of all GOAL_ parameters (if there are more than one)
select ltrim(elem, 'GOAL_')
from (
select unnest(string_to_array(params, '/')) elem
from test
) sub
where elem like 'GOAL_%'
You may try using regular expressions, getting the string between slashes
select substring(your_column from '^.*/(.*)/.*$') from your_table
If you expect to find in that part the GOAL value, use
select substring(your_column from '/GOAL_(.*)/') from your_table

Convert a string representing a timestamp to an actual timestamp in PostgreSQL?

In PostgreSQL: I convert string to timestamp with to_timestamp():
select * from ms_secondaryhealthcarearea
where to_timestamp((COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS')
> to_timestamp('20121128191843','YYYYMMDDHH24MISS')
But I get this error:
ERROR: syntax error at end of input
LINE 1: ...H24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
^
********** Error **********
ERROR: syntax error at end of input
SQL state: 42601
Character: 176
Why? How to convert a string to timestamp?
One too many opening brackets. Try this:
select *
from ms_secondaryhealthcarearea
where to_timestamp(COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
You had two opening brackets at to_timestamp:
where to_timestamp((COA.. -- <-- the second one is not needed!
#ppeterka has pointed out the syntax error.
The more pressing question is: Why store timestamp data as string to begin with? If your circumstances allow, consider converting the column to its proper type:
ALTER TABLE ms_secondaryhealthcarearea
ALTER COLUMN update_datetime TYPE timestamp
USING to_timestamp(update_datetime,'YYYYMMDDHH24MISS');
Or use timestamptz - depending on your requirements.
Another way to convert a string to a timestamp type of PostgreSql is the above,
SELECT to_timestamp('23-11-1986 06:30:00', 'DD-MM-YYYY hh24:mi:ss')::timestamp without time zone;
I had the same requirement as how I read the title. How to convert an epoch timestamp as text to a real timestamp. In my case I extracted one from a json object. So I ended up with a timestamp as text with milliseconds
'1528446110978' (GMT: Friday, June 8, 2018 8:21:50.978 AM)
This is what I tried. Just the latter (ts_ok_with_ms) is exactly right.
SELECT
data->>'expiration' AS expiration,
pg_typeof(data->>'expiration'),
-- to_timestamp(data->>'expiration'), < ERROR: function to_timestamp(text) does not exist
to_timestamp(
(data->>'expiration')::int8
) AS ts_wrong,
to_timestamp(
LEFT(
data->>'expiration',
10
)::int8
) AS ts_ok,
to_timestamp(
LEFT(
data->>'expiration',
10
)::int8
) + (
CASE
WHEN LENGTH(data->>'expiration') = 13
THEN RIGHT(data->>'expiration', 3) ELSE '0'
END||' ms')::interval AS ts_ok_with_ms
FROM (
SELECT '{"expiration": 1528446110978}'::json AS data
) dummy
This is the (transposed) record that is returned:
expiration 1528446110978
pg_typeof text
ts_wrong 50404-07-12 12:09:37.999872+00
ts_ok 2018-06-08 08:21:50+00
ts_ok_with_ms 2018-06-08 08:21:50.978+00
I'm sure I overlooked a simpler version of how to get from a timestamp string in a json object to a real timestamp with ms (ts_ok_with_ms), but I hope this helps nonetheless.
Update: Here's a function for your convenience.
CREATE OR REPLACE FUNCTION data.timestamp_from_text(ts text)
RETURNS timestamptz
LANGUAGE SQL AS
$$
SELECT to_timestamp(LEFT(ts, 10)::int8) +
(
CASE
WHEN LENGTH(ts) = 13
THEN RIGHT(ts, 3) ELSE '0'
END||' ms'
)::interval
$$;