Convert string to timestamp and use timestamp_diff in BigQuery SQL - sql

I have two different strings:
created = 2019-06-30T17:33:09.879350220Z
updated = 2019-09-25 06:42:45
I have to perform TIMESTAMP_DIFF(created_date, updated_date, HOUR) in a condition.
For the second one, CAST(updated AS TIMESTAMP) works but when I do CAST(created AS TIMESTAMP), it shows me an error Invalid Timestamp.
How can I cast the created string to a TIMESTAMP most efficiently?

Yeah, this format is kinda tricky but works with a format like this:
select parse_timestamp('%Y-%m-%dT%H:%M:%E*SZ','2019-06-30T17:33:09.879350220Z')
returns:
2019-06-30 17:33:09.879350 UTC

You can use parse_timestamp().
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S','2019-09-25 06:42:45')

Related

convert date unix to timestamp

I want to convert type date unix to timestamp in Informix.
My column date1 contains values as 1598915961, 1598911249, 1598911255...
expected output: 2020-02-13 15:00:00
How should I do it, please?
In Informix, you can use dbinfo() and 'utc_to_datetime':
select dbinfo('utc_to_datetime', myepoch)
The idea is that you can add the seconds to the date '1970-01-01'. I don't have Informix on-hand, but the syntax is something like this:
select datetime('1970-01-01') + interval date1 second

SELECT query with LIKE fails

I'm using a SQL SELECT query to bring back all rows from a specific date.
The column I'm using is called TimeStamp (datetime)
(An example of data from this column = 01/02/2018 07:55:55)
What I would like is to return all rows from a specific date eg 24/06/2019
I have tried
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02/01/2018 07:55:55'
which returns one row (which is correct as there is only one row containing this data)
If I then try
SELECT top 20 TimeStamp
from Report
where TimeStamp LIKE '02/01/2018%'
I get no results, I have also tried escaping the forward slashes
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02\/01\/2018%'
Most databases support a string function called left(). If I assume that your "timestamp" is a string, then:
where left(timestamp, 10) = '01/02/2018'
However, it should be stored as a date or date/time. If so, then you can do:
where timestamp >= '2018-02-01' and
timestamp < '2018-02-02'
Note the use of standard formatted dates (YYYY-MM-DD). That is the way most databases implement date literals.
In SQL Server, you can also use:
where convert(date, timestamp) = '2018-02-01'
Both this and the previous version will use an index on timestamp, so both are reasonable solutions.
this should work
SELECT TimeStamp FROM report where convert(Date, TimeStamp) = '2019-06-24'
or select timestamp from report where timestamp between '2019-06-24' and '2019-06-25'. This will get you everything between 2019-06-24 00:00:00 and 2019-06-25 00:00:00 thus all records with date 2019-06-24
Convert timestamp value to date.
SELECT TimeStamp
FROM report
WHERE CAST(TimeStamp AS DATE) = '2019-06-24'

I have a date/time stamp field, where I need to pull records just by date

I have a date/time stamp field, where I need to pull records just by date.
Example: All data were records are >= '01/01/2016'.
The data in the field is store in the following format '9/5/2012 7:34:59 AM'
I have tried the following but either I get an error or bad results:
where to_char(start_time) > '01/01/2016' (still gives 2012 records)
where trunc(start_time) > '01/01/2016' (Error: Not a valid month)
In mysql you should use
this is in string canonical format
select * from my_table
where start_time > '2016/01/01'
or
or converting by str_to_date using proper format
select * from my_table
where start_time > str_to_date('01/01/2016', '%d/%m/%Y')
Use to_date to convert the string to date and then do the comparison.
Try this:
where start_time >= to_date('01/01/2016', 'dd/mm/yyyy');
In Oracle (and perhaps other database products) you can use the ANSI date literal, as shown below. You could also use to_date(), but the benefit of the ANSI date literal is that it doesn't require a function call. (Function calls are overhead which consumes time and resources, although calling to_date() just once is not a concern.)
... where start_time >= date '2016-01-01'
Note that in the ANSI standard date literal, the date must be in the exact format YYYY-MM-DD (with dashes and not with slashes or any other separators), since the ANSI date literal does not take a format model.

HIVE - date_format( your_date_column, '%Y-%m-%d %H' )

I'm trying to achieve the MySQL equivalent of date_format( your_date_column, '%Y-%m-%d %H' ) as my_date in Hive. I've tried a few options from Hive date formatting but can't get the format right. I haven't found anything that has helped me yet.
Could I please request someone who may have already bumped into this situation or knows how to do it please?
Recent version of Hive have a date_format() function, it just uses java formatting codes instead of C. Try date_format(your_date_column, 'yyyy-MM-dd HH')
To convert date to given string format you have to use from_unixtime() function of hive
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH') as my_date from table1;
where table1 is the table name present in my hive database.
I hope this help you to achieve date_format( your_date_column, '%Y-%m-%d %H' ) !!!
Let's say you have a column 'birth_day_H' in your table which is in string format,
you should use the following query to filter using birth_day_H
date_Format(birth_day_H, 'yyyy-MM-dd HH')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day_H, 'yyyy-MM-dd HH') = '2019-04-16 10';
I worked around this by using concat(substr(your_date_column,1,13), ':00')
In case the date column has a reserved keyword such as timestamp as in my case, this works - concat(substr(`timestamp`,1,13), ':00')

Convert YYYYMMDD String to Date in Impala

I'm using SQL in Impala to write this query. I'm trying to convert a date string, stored in YYYYMMDD format, into a date format for the purposes of running a query like this:
SELECT datadate,
session_info
FROM database
WHERE datadate >= NOW() - INTERVAL 5 DAY
ORDER BY datadate DESC;
Since the >= NOW() - INTERVAL 5 DAY code won't work with the YYYYMMDD string, I'd like to find a way to convert that into a date format that will work with this type of query. My thought is that it should look something like this (based on similar questions about other SQL query editors), but it's not working in Impala:
SELECT datadate,
session_info,
convert(datetime, '20141008', 102) AS session_date
FROM database
WHERE session_date >= NOW() - INTERVAL 5 DAY
ORDER BY session_date DESC;
Anyone know how to do this in Impala?
EDIT:
I finally found a working solution to the problem. None of the attempts using configurations of CAST or CONVERT would work in Impala, but the below query solves the problem and is fully operational, allowing date math to be performed on a column containing string values:
SELECT datadate,
session_info
FROM database
WHERE datadate >= from_unixtime(unix_timestamp(now() - interval 5 days), 'yyyyMMdd')
GROUP BY datadate
ORDER BY datadate DESC;
Native way:
to_timestamp(cast(date_number AS STRING), 'yyyyMMdd')
See Timestamp Literals on [Link Updated 2020-08-24]:
https://docs.cloudera.com/cdp-private-cloud-base/7.1.3/impala-sql-reference/topics/impala-literals.html
You need to add the dashes to your string so Impala will be able to convert it into a date/timestamp. You can do that with something like:
concat_ws('-', substr(datadate,1,4), substr(datadate,5,2), substr(datadate,7) )
which you can use instead of datadate in your expression.
To ignore hour/minute/second... , use from_timestamp, result 2020-01-01.
select from_timestamp(cast('2020-01-01 01:01:01.000000' as TIMESTAMP),'yyyy-MM-dd');