How to add hours to a time field - amazon-redshift

In amazon redshift I would like to add number of hours to a time field
tt_time hours_add
12:00:00 1
15:30:00 3
16:00:00 6
Expected Output
13:00:00
18:00:00
22:00:00

You can use the DATEADD Redshift function, using "h", "hr" or "hrs" as your first parameter. Documentation for this function can be found here and here.
This sql statement should work for you, assuming you want to create a new table to hold your results:
INSERT into new_table_name SELECT DATEADD(hr,hours_add,tt_time) FROM current_table_name;

Related

Set the time default to 00:00:00 in DB2

I have a DateTime column (activation_dt) in DB2 table and I want to add 1 day to the date part and my output should be "date 00:00:00".
For example:
How it is - 5/9/2001 03:00:00
how it should be - 5/10/2001 00:00:00
I tried using Concat function but is not working. Date part I am doing as "date(activation_dt +1 day) as new_dt"
Please help how should I achieve this is DB2.
You could cast it to a DATE, then TIMESTAMP. Here is the invers, try out the individual parts.
values date(timestamp(current date))
BTW: What is CURRENT TIMEZONE?
Try this
VALUES date_trunc('DAY', CURRENT TIMESTAMP + 1 DAY)
it will always return the next day 00:00:00 - use activation_dt instead of current timestamp

Why is TRY_CAST only working for some records

I have used a TRY_CAST on a Varchar field where dates are stored (I know bad practice but nothing I can control).
The value 12/05/2018 04:30 is being outputted as 2018-12-05 04:30:00.000 which is incorrect as here in the UK 12/05/2018 is 12th May, however the TRY_CAST is outputting a value of 5th Dec.
The TRY_CAST isn't returning any value for 29/05/2018 07:00 (29th May) as I assume it thinks its 5th of the 29th Month which doesn't exists.
,TRY_CAST([Time decision made for caesarean section].VALUE as datetime)
AS DECISION_FOR_CAESAREAN_SECTION_DATE_TIME
The field above is what I am trying to output
Below is some sample data from of the original stored values, all of which are UK (May) dates.
22/05/2018 07:30
12/05/2018 04:30
22/05/2018 00:51
16/05/2018 10:08
21/05/2018 13:03
17/05/2018 11:53
21/05/2018 14:22
29/05/2018 07:00
What is the best method to output these as valid UK datetimes/
Use TRY_CONVERT which offers the style option that TRY_CAST doesn't
SELECT
TRY_CONVERT(datetime, '12/05/2018 04:30', 103),
TRY_CONVERT(datetime, '22/05/2018 07:30', 103),
TRY_CONVERT(datetime, 'gbn', 103)
Style 103 affects the date part only.
Alternatively, you can use SET DATEFORMAT dmy first
SET DATEFORMAT dmy
SELECT
TRY_CAST('12/05/2018 04:30'AS datetime),
TRY_CAST('22/05/2018 07:30'AS datetime),
TRY_CAST('gbn'AS datetime);

how to find number of days since 28th of last month till 27th of current month in db2

I need to generate a report on 28th of every month .
So for that I need to run an autosys job.
In that I have a query with the condition
validation_date >= (number of days since last run)
Could you please help me on this .How can I achieve this condition in DB2 ?
This is a monthly job.So I don't want to hard code my previous run date in the query .At the same time I need to get a condition which satisfies for all the months .
Note :
If the query is running on feb 28th ,then feb 28th is not included. I need to get data from january 28th(included) till feb 27th(included)
similarly for march 28th run ,I need to get data from feb 28th(included) till march 27th(included)...Thanks in advance.Please help
Consider putting your report generation in a procedure, and parameterizing the start and end dates. In other words, have something like this:
create procedure monthly_report(
start_date date,
end_date date
)
language sql
begin
... report queries here ...
end
Now you potentially have something much more flexible (depending on the report requirements). If, in the future, you want to run a report on a different day, or for a different length of time, you will be able to do that.
Once you design it this way, it also may be easier to set the dates in your job scheduling script, rather than in SQL. If you did it in SQL, you could do something like this:
call monthly_report(
(select
year(current timestamp - 2 months) ||'-'||
month(current timestamp - 2 months) ||'-'||
'28' from sysibm.sysdummy1
),
(select
year(current timestamp - 1 month) ||'-'||
month(current timestamp - 1 month) ||'-'||
'27' from sysibm.sysdummy1
)
)
You may need to tweak it to handle some edge cases (I'm not exactly sure if you care what happens if it runs on the 29th of the month, and if so, how to handle it). But you get the basic approach.
You can use DAY() function that extracts day of month from date and you can use it for triggering job. for example where day(param)=28.
other two parameters can be calculated with date calculation , here is example for trigger , date_to value and date_from value
select day(timestamp_format(20170228,'yyyyMMdd') ),timestamp_format(20170228,'yyyyMMdd')- 1 DAY,timestamp_format(20170228,'yyyyMMdd') -1 month from sysibm.sysdummy1;
if your parameter/column is date/timestamp you can remove timestamp_format(20170228,'yyyyMMdd') function and just put your column/parameter

Postgresql ISOYEAR first date

How to get the start date of ISOYEAR in postgresql?
For example, i have a date 2012-01-01, isoyear is 2011 and it isoyear starts at 2011-01-03 and ends at 2012-01-01.
There is different ways to get isoyear, but i have no idea how to get date that iso year begins.
select extract(isoyear from '01.01.2012'::date)
select to_char('01.01.2012'::date,'IYYY')
Ask it to convert the first "ISO day" of the "ISO year":
=> SELECT to_date('2011-0001', 'IYYY-IDDD');
to_date
------------
2011-01-03

To get the weekday from TIMESTAMP in hive

I need to calculate mean sales for sunday. Values for the column salesdate(timestamp) are:
2012-01-01 09:00:00
2012-01-01 09:00:00
2012-01-01 09:00:00
...........
I have extracted the date part using to_date().Now how to get weekday(like sunday) from this date in hive?
Please guide.
You can use a combination of unix_timestamp, and from_unixtime UDFs.
from_unixtime(unix_timestamp(col), 'EEEE')
If you check the documentation for SimpleDateFormat, which from_unixtime uses, you can see that "EEEE" is the code for the full name of the day of the week. "EEE" gets you the abbreviated version, i.e. "Sun" or "Mon".
There is no OOTB feature to achieve this as of now. A ticket is open though.
You need to write a UDF for this. Or, you could also try the patch available with the above mentioned ticket.
HTH
In Hive you can also use the below method to solve this problem in very elegant way and its performance is very good.
from_unixtime accepts the 1st argument in int format:
date_format(from_unixtime(col(timestampinseconds),'yyyy-MM-dd'),'EEEE')
You can also test it like this:
select date_format(from_unixtime(1531372789,'yyyy-MM-dd'),'EEEE');
Output:
Thursday
I hope it serves your purpose.
Just a suggestion.. you can take a low date (lower than the minimum date in your data), which is a Sunday, in 'yyyy-mm-dd' format. Use DATEDIFF() function to find the difference between the date value in your data (in 'yyyy-mm-dd' format) and this low date. Calculate modulo 7 of the datediff output. This will be 0 for Sunday, 1 for Monday, and so on..
select extract(dayofweek from from_unixtime(unix_timestamp));