PostreSQL- How to convert float containing a number of seconds to MM:SS.sss - postgresql

I have a float8 that contains a number of seconds ie 65.455. I was trying to format a column in a view so that it would read as 1:05.455.
Using postgres command like this:
TO_CHAR((user_data.totaltime || ' second')::interval, 'MI:SS')
I can format it as 1:05 but i loose the precision i need.
Anyone know of a way to achieve what i need ? It doesn't look like interval allows the formatting of milliseconds.

Try justify_interval:
SELECT justify_interval(
CAST(65.455::text || ' seconds' AS interval)
);

Here is a rather brute force you may do this, using the base string functions:
SELECT
FLOOR(65.455 / 60)::text || ':' || LPAD(FLOOR(65.455 % 60)::text, 2, '0') ||
TRIM(LEADING '0' FROM (65.455 % 1)::text);
This outputs:
1:05.455
Demo

Related

How to pass an interval parameter to a prepared statement?

I want to delete all database entries in my Postgres database that are older than X minutes. My prepared statement in go looks like this:
delete
from my_table
where expires < (to_timestamp($1) - '$2 minutes'::interval);
How can I correctly pass the second parameter $2?
PS: I know there's different statements to solve the problem, but I am explicitly interested in how to pass parameters that are quoted.
There is no way to interpolate a parameter into a string literal.
A possible solution for your case would be to multiply a number with an interval:
where expires < (to_timestamp($1) - $2 * '1 minute'::interval)
You can cast the parameter to text and then concatenate it with the string ' minutes'.
delete from my_table
where expires < (to_timestamp($1) - ($2::text || ' minutes')::interval
UPDATE: actually, since postgres does have a any || text operator, the result of which is text, you shouldn't need to type cast the parameter.
You can use make_interval
delete from my_table
where expires < (to_timestamp($1) - make_interval(mins => $2));
You can also parametrize the entire interval string instead. This removes the need for an intermediate cast to text and concat.
query := `
delete from my_table
where expires < (to_timestamp($1) - $2::interval);
`
interval := fmt.Sprintf("%d minutes", mins)
db.Exec(query, timestamp, interval)

why do i have a date error in my sql query?

I am doing an SQL SELECT query but I have the error message:
"SQL Error [22007]: [SQL0181] A value of date, time, or timestamp
string is incorrect."
Here is my request:
SELECT *
FROM ROXDTA400.STKF0300 A
JOIN ROXDTA400.TABJ00141 B ON A.STNSIT = B.CDSITE
WHERE ( A.STNLIB <> '-- Trémie --'
AND A.STNSIT <> 40
AND DATE(LEFT(STNDAV,4) || '-' || substr(STNDAV,5,2) || '-' || RIGHT(STNDAV,2))
BETWEEN DATE('2019-01-01') AND DATE('2019-01-04') );
The problem seems to come from the date created with the STNDAV field, because if I replace with for example DATE ('2019-01-03'), it works.
DATE (LEFT (STNDAV, 4) || '-' || substr (STNDAV, 5,2) || '-' || RIGHT (STNDAV, 2)) Gives me the correct date format.
Where would the problem come from?
thank you,
Ensure that dates stored in STNDAV are valid. I mean, check there is any invalid date such as February 30th or '99999999'. If the source is an IBM i (iSeries or AS/400), it will be faster if you avoid functions in the WHERE portion, so STNDAV BETWEEN '20190101' AND '20190104' will perform better.

How to convert decimal to varchar with leading zero in DB2?

I want to convert the result of a division to a character string in DB2.
My result when I do '0.24'||'%' is '.24%'
I don't understand how to get exactly the output '0.24%'
hi try Something like this
case when myval<1 and myval>0 then '0' || cast(myval as char(15))
when myval>-1 and myval<0 then '-0' || cast(abs(myval) as char(15))
else cast(myval as char(15)) end myvaltochar
try this
to_char( yourzone
repeat('0', length(trim(int(abs(yourzone))))) || '.'||
repeat('0', length(trim(
INTEGER(SUBSTR(CHAR(-123.36),LOCATE(',',CHAR(yourzone))+1)))))
)
SELECT VARCHAR_FORMAT(24.0/100,'0.00')||'%' ...
If you are dividing integer types, you'll have to cast them as DOUBLE() otherwise the result will be 0

return one string from colums data

I've result set from query select * from personal."phoneNumbers" like this
prefix
pref |number
-----|--------
"12 "|"4589524"
"077"|"7090701"
"050"|"2561024"
But I want to return data like
(12) 4589524;(077) 7090701; (050) 2561024
How to do this with postgresql ?
You can use the string operators and functions to construct a single phone number in the format that you want:
'(' || btrim(pref) || ') ' || number
This, obviously, yields a string for each record that you process. You can then use the aggregation function string_agg() to string (no pun intended) the extended phone numbers from all the records together into one, with the appropriate separator between phone numbers:
SELECT string_agg('(' || btrim(pref) || ') ' || number, '; ') AS pref_number
FROM personal."phoneNumbers"

Frontbase date formatting functions

I've searched all over and can't find any info in Frontbase documentation or, for that matter, SQL92-related docs...does Frontbase have functions equivalent to datepart/date_part, or date_format as found in other RDBMSes? I need to output a timestamp column as a formatted string, and the correct syntax for Frontbase eludes me.
There may be a better way to do it, but a combination of cast, extract and string concatenation gave me the desired result:
SELECT CAST(EXTRACT(month FROM mytimestampcol) AS VARCHAR(2)) || '/' || CAST(EXTRACT(day FROM mytimestampcol) AS VARCHAR(2)) || '/' || CAST(EXTRACT(year FROM mytimestampcol) AS VARCHAR(4)) AS "Begin Date" FROM mytable
Yields "mm/dd/yyy" formatting.
One of many SQL92 function references: http://www.faircom.com/doc/sqlref/#12959.htm