TeradataSQL: Time to String, Add to Date and Compare to Another Time and Data - teradata

I'm trying to figure out the cleanest way to do a comparison in Teradata SQL Assistant. I have the scheduled start date (TimeStamp), the Schedule start time (varchar), actual start and end times (TimeStamp). I need to consolidate the scheduled start date and time and be able to compare it to the actual start and end date and time without modifying the original data (because it's not mine). I realize that the Scheduled Start Time [SST] is in a 24 hour time format with a AM/PM suffix, but like I said before, I can't change that.
I tried to do select cast(substr(scheduled_start_date,1,5) as TIMESTAMP(0)) from DB.TBL but am getting the "Invalid timestamp" error. There is example table data below.
Sch Start Date Sch Start Time Actual Start Actual End
09/11/2017 00:00:00 11:30 AM 09/11/2017 11:34:16 09/11/2017 11:58:00
05/26/2017 00:00:00 15:30 PM 05/26/2017 15:40:00 05/26/2017 15:55:15
11/06/2017 00:00:00 19:30 PM 11/06/2017 21:25:00 11/06/2017 21:45:00
Thanks!

You need to cast the schedule start time as an Interval, then you can easily add it to the start date:
scheduled_start_date
+ Cast(Substr(scheduled_start_time, 1,5) AS INTERVAL HOUR TO MINUTE)
A start date which is a timestamp seems to indicate this was ported from Oracle/SQL Server?
And a 24 hour time format with a AM/PM suffix is also quite strange.

A couple things to try:
Convert the separate Scheduled Date and Scheduled Time fields into strings, concatenate them, and feed that into a TIMESTAMP CAST. Something like:
SELECT
CAST(CAST(Scheduled_Date AS DATE) AS VARCHAR(25)) AS Date_String,
CAST(CAST(Scheduled_Time AS TIME FORMAT 'HH:MM BB') AS VARCHAR(25)) AS Time_String,
CAST(TRIM(Date_String) || ' ' || TRIM(Time_String) AS TIMESTAMP(0)) AS MyTimestamp
Cast the Scheduled Time field as a TIME data type. Cast the Scheduled Date field as a DATE data type. Then somehow combine the two into a TIMESTAMP field -- either with a CAST or some kind of timestamp constructor function (not sure if this is possible)
Option 1 should work for sure as long as you properly format the strings. Try to avoid using SUBSTRING and instead use FORMAT to cast as DATE/TIME fields. Not sure about Option 2. Take a look at these link for how to format DATE/TIME fields using the FORMAT clause:
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference%2FB035-1143-160K%2Fmuq1472241377538.html%23wwID0EPHKR
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference/B035-1143-160K/cmy1472241389785.html
Sorry, I don't have access to a TD system to test it out. Let me know if you have any luck.

Related

Calculate a date in behind 24 hours Hive

My demand is really so silly, so basically I need to go back in time 24 hours in a timestamp column in Hive.
So far, I have tried two different ways but it's not going thru:
select
recordDate, --original date
cast(date_sub(cast(recorddate as timestamp),1) as timestamp), -- going one day behind without hour
cast((cast(cast(recorddate as timestamp) AS bigint)-1*3600) as timestamp) -- crazy year
from mtmbuckets.servpro_agents_events limit 10;
My output looks:
I appreciate the support you can give me.
thanks
There is not straight forward function in hive .
1 Create UDF to do so .
or
Convert date in no of second and do you calculation( -24 *60*60) sec then change back int to data.
use from_unixtime and unix_timestamp to achieve below code.
select from_unixtime(unix_timestamp(recorddate) - 86400)
from mtmbuckets.servpro_agen ts_events limit 10;;
From_unixtime
Convert time string with given pattern to Unix time stamp (in seconds) The result of this function is in seconds.
Unix_timestamp
Converts time string in format yyyy-MM-dd HH:mm:ss to Unix timestamp (in seconds), using the default timezone and the default locale, return 0 if fail: unix_timestamp('2009-03-20 11:30:01') = 1237573801

I need a sqlite equivalent of the folling msaccess query

Select distinct Format(DateAdd(""s""," & columnname & ",""1/1/1980 12:00:00 AM""), 'dd-MMM-yyyy') as A
I have assumed that the seconds to add and the original date are hard coded values below whilst awaiting clarifications requested in the comments.
To add a number of seconds to a date you can use:
select datetime('1980-01-01 00:00:00', "345000 seconds");
This gives the result: 1980-01-04 23:50:00
The example above is just under 4 days in seconds, if you want to truncate the result to just the date as implied by the query in your questions then you can wrap this inside a date function. However, this would give the result in the format "YYYY-MM-DD" rather than "DD-MMM-YYYY" as your access query does.
Unfortunately I cannot find any native SQLite function to convert a numeric month value to mmm format. You can do this manually with replace (similar to the answer to this question), but this is a bit messy.
If you are happy to live with the numeric months then you can simply use:
select strftime('%d-%m-%Y', '1980-01-01 00:00:00', "345000 seconds");
This gives the result: 04-01-1980
More information on the SQLite date / time functions can be found here.

Format Datetime in Business Objects

So I know I can use =FormatDate(MyDate ,"mm/dd/yy") to turn a date into a string. I am then trying to add on a time:
=FormatDate(AdminDate ,"mm/dd/yy") + MyTime
Which works, however, I need to format this back to a datetime field (as I need to compare against a preexisting datetime field). I try using Todate(), but the documentation is very light, and provides very little on what is acceptable in the formatting of the date area, and nothing in the way of time!
I have attempted:
=ToDate(FormatDate(MyDate ,"mm/dd/yy HH:mm:ss") + MyTime ,"mm/dd/yy HH:mm:ss")
but this will only work when there is no time (and it just nulls out the time) and any row with a time will return a #ERROR
Anyone have an insight on formatting datetimes?
Thanks
The correct way to use the FormatDate command to output date and time components together in 12 hour format is:
=FormatDate(AdminDate; "MM/dd/yy hh:mm:ss a")
and in 24 hour format is:
=FormatDate(AdminDate; "MM/dd/yy HH:mm:ss")
Note MM is used in months and mm in minutes.

Convert unix timestamp to human readable time in mysql

We are trying to convert unix timestamp to human readable time when running mysql commands.
For the unix date we have this working command
select FROM_UNIXTIME(registered) AS "ResolutionDateLine" from tickets
which gives us an readable date like
2012-12-03 09:41:00
But we do also have unix timestamp "seconds" that we need to convert, using the same line as above we get 1970-01-01 01:00:00 but the actual value should be 89 days, 23 hours, 22 minutes and 34 seconds.
Then we tried
select FROM_UNIXTIME(firstresponsetime, "%dd, %Hh, %Im") AS "Response" from tickets
with this result:
01d, 00h, 12m
Does anyone know how to convert this correctly in the mysql command?
Use SEC_TO_TIME (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time) to convert a duration in seconds to a HH:mm:ss notation.
select sec_to_time(3500);
results in
00:58:20
Your will be like
select FROM_UNIXTIME(firstresponsetime, '%d-%m-%Y %H:%i:%s') AS Response from tickets
or you can customize it by change second parameter.
for more please check below link:-
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_from-unixtime

How to enter in DateTime in Oracle

Some scope creep occured in the last couple of days and now I have to squeeze in date time. The Oracle stored procedure has a Date field (same with the table). Every time I try to enter in the date time value I get this refrain from the exception thrower:
ORA-01830: date format picture ends before converting entire input string
ORA-06512: at line 56
Here is what I try to enter:
SPECIALIST_APPT_DATETIMEIN := '09/Sep/1990 00:00:00'
Here is the param definition I try to squeeze it into:
PCP_APPOINTMENT_DATETIME`in `DATE`
It looks like you just need to use the TO_DATE function to convert the string to a date.
SPECIALIST_APPT_DATETIMEIN := to_date('09/Sep/1990 00:00:00',
'DD/MON/YYYY HH24:MI:SS' );
assuming that you intend to enter times in the 24-hour format (i.e. 17:30:00 for 5:30pm).

Resources