Convert unix timestamp to human readable time in mysql - unix

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

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

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

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.

IMPALA date formats - Convert full timestamp to hourly timestamp

I need to take a full timestamp column (Format: YYYY-MM-DD HH:MM:SS.SSSS) and convert it into hourly timestamp (Format: YYYY-MM-DD HH:00:00)
For example:
I want to convert my existing date:
2016-02-26 04:00:07.766304000
into:
2016-02-26 04:00:00
I tried to use unix_timestamp() and from_unixtime() function but it is too confusing :X
Does someone know how to easily do it?
Many Thanks for the helpers :)
You can use to_date() to get the date of the time and hour() to get the hour of the time, and then concat them together if you do not want to use unix_timestamp() or from_unixtime().
Suppose 'submit_time' looks like '2016-02-26 04:00:07.766304000'.
concat(to_date(submit_time),' ',cast(hour(submit_time) as string),':00:00')
will gives the answer.

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.

Get yesterday date in Unix - KSH script

The below command is used for getting the yerterdays date in Unix Ksh on HP UX
DATE_STAMP=`TZ=CST+24 date +%m/%d/%Y`
Can somebody let me know what does "CST + 24 date " in above command do?
That command sets the timezone to CST+24 and returns the date in that timezone.
if you are looking for a command to find out yesterday's date, you are better of using the TZ trick esp. if you are in a timezone that observes DST.
use perl one liner instead.
#this takes local time and substracts a day(24*60*60 seconds) and formats the time.
echo `perl -e 'use POSIX; print strftime "%m/%d/%Y%", localtime time-86400;'`
Just a guess on your command - since its yesterday at CST+24 timezone the command returns yesterday's date and if you use CST-24, it retunrs tomorrow's date since the date translates to tomorrows date at CST-24 timezone.
VARIABLE=VALUE COMMAND means that you set the environment variable VARIABLE to VALUE but not persistent but only for the executed command COMMAND.
In your example that means: Execute the date command with the environment variable TZ set to CST+24 (which is Central Standard Time plus 24 hours).
Check out this page http://www.kodkast.com/blogs/unix-shell-scripting/how-to-get-yesterdays-date where you can find out yesterday's date as well as any other previous date in unix shell scripting.

Resources