IMPALA date formats - Convert full timestamp to hourly timestamp - datetime

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.

Related

Converting Datetimes returns Null

I'm trying to convert a datetime that looks like this: 2017-09-19T07:00:00-07:00 into EST, but i keep getting Null values when using the hive built in UTC conversion.
I've tried using a regular expression to parse the date:
date_format(from_unixtime(unix_timestamp(2017-09-19T07:00:00-07:00, "yyyy-MM-dd'T'HH:mm:ss")
- (cast(regexp_extract(regexp_extract(2017-09-19T07:00:00-07:00, '(-[0-9][0-9]:[0-9][0-9])$', 1),'(-[0-9][0-9])',1) as int)*3600) -18000),'YYYY-MM-dd HH:mm')
but that's not good, since there's an hourly difference based on the time of year.
I've also tried:
FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(2017-09-19T07:00:00-07:00, "yyyy-MM-dd'T'hh:mm:ss:SSS'ZZZZZ'") * 1000, 'EST')
and
FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(2017-09-19T07:00:00-07:00, "yyyy-MM-dd'T'hh:mm:ss:SSS'Z'") * 1000, 'EST')
but that appears to not work either. What am I doing wrong?
I think that this method needs the date as a string like this:
date_format(from_unixtime(unix_timestamp('2017-09-19T07:00:00-07:00', "yyyy-MM-dd'T'HH:mm:ss")
Normally, the date formats are for strings, not for integers or numbers.
I found the answer on my own by combining the two ways of running the query.
date_format(
FROM_UTC_TIMESTAMP(
(unix_timestamp('2017-09-19T07:00:00-07:00', "yyyy-MM-dd'T'HH:mm:ss")
+ (cast(
regexp_extract(
regexp_extract('2017-09-19T07:00:00-07:00', '(-[0-9][0-9]:[0-9][0-9])$',1),'(-[0-9][0-9])',1) as int)
*-3600)
)*1000 ,'America/New York')
,'YYYY-MM-dd HH:mm:ss')
You are getting NULL because the pattern (format of the date and time) you have provided is not matching with the actual date time value. Correcting the date time format in your query would resolve this issue:
select from_unixtime(UNIX_TIMESTAMP("2017-09-19T07:00:00-07:00", "yyyy-MM-dd'T'HH:mm:ssXXX"), "yyyy-MM-dd HH:mm:ss");
Check out this link to know more about the date time patterns: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Can't format the date using moment.js

Can't format the below date using moment.js, the below statement returns Invalid Date
moment('20171206T062406927Z').format('D-MMM-YYYY');
Please help me on this.
You need to tell moment which format your date string is in:
moment('20171206T062406927Z', 'YYYYMMDD[T]HHmmssSSSZ', true).format('D-MMM-YYYY');
Edit: updated as per #VincenzoC comment to ensure the timestamp is parsed in UTC
Also fix: use HH for 24-hour format (not hh), and pass a third true parameter to ensure the timestamp is parsed in strict mode.

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.

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.

C# format of date issue

My problem: I need to get date format as "mm/dd/yyyy"
Scenario:
I have declared DateBirth as nullable DateTime.
The value I get when I use:
AdvancedObj.DateBirth .Value.ToString()
is: "13/03/2013 00:00:00"
The value I get when I use
AdvancedObj.DateBirth .Value.ToString(CultureInfo.InvariantCulture)
is :"03/13/2013 00:00:00"//This is roughly correct but, I do not need 00:00:00
I have tried this as well, but the format is correct and value is incorrect.
AdvancedObj.DateBirth.Value.ToString("dd/mm/yyyy",CultureInfo.GetCultureInfo("en-Us"))
**"13/00/2013"**
Can anybody point me, what am I missing?
Use the right format string for months - it is MM. mm is for minutes:
AdvancedObj.DateBirth.Value.ToString("MM/dd/yyyy",CultureInfo.InvariantCulture)
Also, order them correctly as above - if you want months before days, use MM/dd/yyyy, if the other way around, dd/MM/yyyy.
I suggest you take a good long read of Custom Date and Time Format Strings on MSDN.
Month are 'M'. 'm' is for minutes.
"dd/MM/yyyy"

Resources