How to get large number in Amazon Redshift? - amazon-redshift

I am trying to get epochtime (13 digits) after subtracting from task_end_time column using something like
((task_end_time - to_date ('01 Jan 1970 00:00:00', 'DD Mon YYYY HH24:MI:SS')) * 24 * 3600* 1000)::bigint AS "log_datetime"
This gives me below exception
ERROR: XX000: Integer data overflow (multiplication)

seems like all you actually need to do is
extract('epoch' from task_end_time) AS "log_datetime"

Related

Wrong day when using day()-formula with format - PowerBI

I'm trying to find out the weekday i.e Mon, Tue, Wed etc. from a date-range formatted as yyyy mm dd
I tried to use the formula format(day(Date Table),"ddd"), but the weekday is wrong. In my example, the output of 2020.01.01 gives Sunday, but it should be Wednesday.
I think your formula is wrong:
Instead of
format(day(Date Table),"ddd")
Use
format(<Target Table>[<date column>],"ddd")
I.e. Omit the DAX DAY call. This is resulting in the day of the month (1..31) being passed to the format function.
When you use the DAY function in DAX, it returns the day of the month (1 through 31).
Thus DAY ( DATE ( 2020, 1, 1) ) = 1 which means you're trying to format the number 1 as a date. Integers are interpreted as days since 1899/12/30 when treated as a date, so 1 corresponds to 1899/12/31, which happened to be a Sunday. Thus FORMAT(1, "ddd") = "Sun".
There's no reason to get DAY involved here. You can simply write
Day = FORMAT ( 'Calendar'[Date], "ddd" )

hour() function of excel in postgres (equivalent)

I am working recently with postgres and I have to make several calculations. However I have not been able to imitate the HOUR () function of Excel, I read the official information but it did not help me much.
The function receives a decimal and obtains the hour, minutes and seconds of the same, example the decimal 0,99988426 returns 11:59:50. Try doing this in postgres (i use PostgreSQL 10.4) with the to_timestamp function: select to_char (to_timestamp (0.99988426), 'HH24: MI: SS'); this return 19:00:00. Surely I am omitting something, some idea of how to solve this?
24:00:00 or 86400 seconds = 1
Half day(12:00 noon) or 43200 seconds = 43200/86400 = 0.5
11:59:50 or 86390 seconds = 86390/86400 = 0.99988426
So to convert your decimal value to time, all you have to do is multiply it with 86400 which will give you seconds and convert it to your format in following ways:
SELECT TO_CHAR((0.99988426 * 86400) * '1 second'::interval, 'HH24:MI:SS');
SELECT (0.99988426 * 86400) * interval '1 sec';
There are two major differences to handle:
Excel does not consider the time zone. The serial date 0 starts at 0h00, but Postgres uses the time zone so it becomes 19h. You would need to use UTC in Postgres result to have the same as in Excel.
select to_char (to_timestamp (0), 'HH24: MI: SS'),to_char (to_timestamp (0) AT TIME ZONE 'UTC', 'HH24: MI: SS');
to_char | to_char
------------+------------
19: 00: 00 | 00: 00: 00
Excel considers that 1 is one day, while Postgres considers 1 as 1 second. To get the same behavior, multiply your number by the 86400, i.e. the number of seconds in a day
select to_char (to_timestamp (0.99988426*86400) AT TIME ZONE 'UTC', 'HH24: MI: SS');
to_char
------------
23: 59: 50
(1 row)

Can someone show me how I can output hrs:mins:secs in mySQL SELECT statement below?

Here is the picture of how I would like it to be displayed
Here is what I have so far when use the DATE_ADD function in MySQL Workbench 6.3, but I have been struggling to output the hrs:mins:secs (you don't see the code for that output because I didn't write it here). I know that I can use INTERVAL HOUR_SECOND to display hrs:mins:secs but I don't understand how it works.
SELECT DATE_ADD('2017-01-26', INTERVAL 31 DAY) AS '31 Days';
I know this SELECT statement above will output 31 days from the specified date indicated above, but what do I need to do to output the hrs:mins:secs along with the 31 days from the date in the SELECT statement?
You can do it using DATE_FORMAT(). Examples:
Add function
/*
* Adds a particular interval to given date
*/
SELECT DATE_ADD('2017-01-26', INTERVAL 31 DAY) AS '31 Days';
Output:
31 Days
2017-02-26
Format function
/*
* Formats a particular date object
*/
SELECT DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
AS 'Now';
Output:
Now
Jan 28 2017 04:29 PM
Format and add:
/*
* Adds and Formats date object
*/
SELECT DATE_FORMAT(DATE_ADD('2017-01-26', INTERVAL 31 DAY),'%b %d %Y %h:%i %p')
AS 'Formatted date';
Output:
Formatted date
Feb 26 2017 12:00 AM
Additionally, here is the sqlfiddle for the example: http://sqlfiddle.com/#!9/9eecb7d/92948
Also check the various formats to further tailor to your needs: http://www.w3schools.com/sql/func_date_format.asp

Casting a string to Date and Time in PostgreSQL

I have a table in GreenPlum (PostgreSQL) with all fields as sting, and I want to edit the types :
To do this I created a view :
CREATE VIEW typed_view AS
SELECT CAST(sid AS bigint), CAST(gid AS bigint),
...
But I have a problem with the Date and Time fields, I tried this command but it didn't work :
to_utc_timestamp(from_unixtime(unix_timestamp(eventdatetime,"yyyy-MM-dd
HH:mm:ss")),'UTC') AS eventdatetime,
After that I tried the PostgreSQL notation :
to_timestamp(eventdatetime, 'YYYY Mon DD HH24 MI SS') AS eventdatetime,
But still not working.
Anyone knows how to convert it ?
I also have this command that is not working :
CASE WHEN fix = "True" THEN TRUE ELSE FALSE END AS fix,
Thanks in advance
You didn't provide example data so I'm going to assume your data looks like "YYYY Mon DD HH24 MI SS". So January 4, 2016 at 2:15:20 PM would look like '2016 Jan 04 14 15 20' in your data. So with this example data, the conversion would look like this:
gpadmin=# select to_timestamp('2016 Jan 04 14 15 20', 'yyyy mon dd hh24 mi ss') as col1;
col1
------------------------
2016-01-04 14:15:20-05
(1 row)
Now this is a timestamp which also include the timezone offset which for my server is -5. To convert this to a timestamp without the timezone, you just add ::timestamptz.
gpadmin=# select to_timestamp('2016 Jan 04 14 15 20', 'yyyy mon dd hh24 mi ss')::timestamp as col1;
col1
---------------------
2016-01-04 14:15:20
(1 row)
A very important note on this. It is costly to convert data from a string to a different datatype. That is the same in all databases too. It is better to incur the expense of this conversion once rather than doing it for every SELECT statement. So, I also suggest you materialize this transformation into a physical table rather than using a VIEW.

How to convert a column in seconds to dateformat in Teradata?

I have a column which is of bigint datatype(in seconds) which should be added to a date, so i need to convert this column into dateformat.
The arithmetic must be done against a timestamp data type in Teradata. The date data type does not have a time element associated with it. The following SQL should help point you in the right direction:
SELECT CAST(CAST(1234 AS BIGINT) AS INTERVAL SECOND(4)) AS Seconds_
, CURRENT_TIMESTAMP(0) AS CurrentTimestamp_
, CURRENT_TIMESTAMP + Seconds_ AS NewTimeStamp
If the number of seconds is less than 864000000 you can simply use interval arithmetic:
CAST(col AS TIMESTAMP) + (bigintcol * INTERVAL '0000 00:00:01' DAY TO SECOND)
Based on your other question your input is a Unixtime, those are two functions for converting them from/to Teradata timestamps:
/**********
Converting Unix/POSIX time to a Timestamp
Unix time: Number of seconds since 1970-01-01 00:00:00 UTC not counting leap seconds (currently 24 in 2011)
Also working for negative numbers.
The maximum range of Timestamps is based on the range of INTEGERs:
1901-12-13 20:45:52 (-2147483648) to 2038-01-19 03:14:07 (2147483647)
Can be changed to use BIGINT instead of INTEGER
20101211 initial version - Dieter Noeth
**********/
REPLACE FUNCTION UnixTime_to_TimeStamp (UnixTime INT)
RETURNS TimeStamp(0)
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN
CAST(DATE '1970-01-01' + (UnixTime / 86400) AS TIMESTAMP(0))
+ ((UnixTime MOD 86400) * INTERVAL '00:00:01' HOUR TO SECOND)
;
SELECT
UnixTime_to_TimeStamp(-2147483648)
,UnixTime_to_TimeStamp(0)
,UnixTime_to_TimeStamp(2147483647)
;
/**********
Converting a Timestamp to Unix/POSIX time
Unix time: Number of seconds since 1970-01-01 00:00:00 UTC not counting leap seconds (currently 24 in 2011)
The maximum range of Timestamps is based on the range of INTEGERs:
1901-12-13 20:45:52 (-2147483648) to 2038-01-19 03:14:07 (2147483647)
Can be changed to use BIGINT instead of INTEGER
20101211 initial version - Dieter Noeth
**********/
REPLACE FUNCTION TimeStamp_to_UnixTime (ts TimeStamp(6))
RETURNS INTEGER
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN
(CAST(ts AS DATE) - DATE '1970-01-01') * 86400
+ (EXTRACT(HOUR FROM ts) * 3600)
+ (EXTRACT(MINUTE FROM ts) * 60)
+ (EXTRACT(SECOND FROM ts))
;
SELECT
TimeStamp_to_UnixTime(TIMESTAMP '1901-12-13 20:45:52')
,TimeStamp_to_UnixTime(CURRENT_TIMESTAMP)
,TimeStamp_to_UnixTime(TIMESTAMP '2038-01-19 03:14:07')
;