I am converting from SQL server to PostgreSQL. I have a function that take a float and converts it to a time. For example if the float number is 12.5 it converts to 12:30. Is there anyway to do this in postgres?
Thanks
Something like this?
select to_char(to_timestamp((12.5) * 60), 'MI:SS');
DEMO
If seconds is the only thing you need, you can write something like this:
select date_part('second', to_timestamp((12.37) * 60))::int;
or
select to_char(to_timestamp((12.37) * 60), 'SS');
In my personal case, this worked:
to_char(to_timestamp(table.time_stamp/1000), 'DD/MM/YYYY HH24:MI:SS')
You can even change the date format (for instance, 'mm/dd/yyyy' or 'dd-mm-yyyy) and the space between the date and the time (another example: 'dd/mm/yyyy | HH24:MI:SS')
SELECT 6.75*'1 HOUR'::INTERVAL
Related
I'm using Postgres with Rails. I've two string fields which store date like this, field_1 = "2020.06.09", I 'm comparing them with TO_DATE function and they are working finr but now I want to compare them after adding 5 months in one field. What I've tried so far is:
select * from users as u where TO_DATE(u.field_1, 'YYYY MM DD') > DATEADD(MONTH, 5, TO_DATE(u.field_2, 'YYYY MM DD'))
can anyone help me on this?
Thanks in advance.
There is no dateadd() in Postgres (or standard SQL), you need to add an interval:
select *
from users u
where TO_DATE(u.field_1, 'YYYY MM DD') > to_date(u.field_2, 'yyyy mm dd') + interval '5 month';
I am trying to covert a timestamp value in my data to 'yyyy-mm-dd HH:mm:ss' using UNIXTIMEFORMAT formula but it is giving a wrong result as you can see in the screenshot here.
Unixtime 1592574691 translates to ==> 2020-06-19 17:51:31 but dataprep is converting to 1970-22-19 10:22:54
Your click_time is in seconds but UNIXTIMEFORMAT needs miliseconds. Try to multiply your column by 1000
UNIXTIMEFORMAT($col * 1000, 'yyyy-mm-dd HH:mm:ss')
I'm trying to select records from a DB2 Iseries system where the date field is greater than the first of this year.
However, the date fields I'm selecting from are actually PACKED fields, not true dates.
I'm trying to convert them to YYYY-MM-DD format and get everything greater than '2018-01-01' but no matter what I try it says it's invalid.
Currently trying this:
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYY-MM-DD') >= '2018-01-01';
it says expression not valid using format string specified.
Any ideas?
char(dateshp) is going to return a string like '20180319'
So your format string should not include the dashes.. 'YYYYMMDD'
example:
select to_date(char(20180101), 'YYYYMMDD')
from sysibm.sysdummy1;
So your code should be
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYYMMDD') >= '2018-01-01';
Charles gave you a solution that converts the Packed date to a date field, and if you are comparing to another date field, this is a good solution. But if you are comparing to a constant value or another numeric field, you could just use something like this:
select *
from table1
where val = 145
and dateShp >= 20180101;
I am looking to get the row which has the date is equal to 2017-01-12. The date has field type timestamp without time zone. I tried
SELECT * FROM tabl WHERE date = 2017-01-12;
What is the correct syntax?
try:
SELECT * FROM tabl WHERE date = '2017-01-12';
If that doesn't work, try an explicit cast:
SELECT *
FROM tabl
WHERE date = DATE '2017-01-12';
I have a SELECT statement that I am trying to convert from Netezza SQL to Impala SQL. The output looks something like 140612, which is a date that is obtained by subtracting 7 from the current date and then pulling out the monday of that week.
I need to have this readable for Impala, then format it, then turn it into a string.
The query is :
TO_CHAR(next_day(DATE(a.date)-7, 'Monday'), 'YYMMDD') AS START_DATE
Assuming a.date is a timestamp, and T is the day of the week (1 = Sunday, 7 = Saturday; for your example above, Monday = 2, so T = 2) you should be able to use use
date_add(a.date, 7 - pmod(dayofweek(a.date) - T, 7));
in place of next_day in the above query. Check out the documentation on Impala's built-in date and time functions for more detail.