BigQuery convert String to Date - sql

In my dataset, one column called timestamp was created with datatype as String.
It contains values like:
2018-05-30T12:56:27:487+0200
I want to construct a query where I can fetch all column from the dataset table based on the date in 'YYYY-MM-DD' format.
I want to use it in where clause with DATE Range between.
Can you guide?
Thank you.

convert String to Date
Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT '2018-05-30T12:56:27.487+0200' ts UNION ALL
SELECT '2018-05-30T01:56:27.487+0200'
)
SELECT ts AS ts_as_string,
PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts) ts_as_timestamp,
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) ts_as_date
FROM `project.dataset.table`
with result
ts_as_string ts_as_timestamp ts_as_date
2018-05-30T12:56:27.487+0200 2018-05-30 10:56:27.487 UTC 2018-05-30
2018-05-30T01:56:27.487+0200 2018-05-29 23:56:27.487 UTC 2018-05-29
As you can see - first i am parsing timestamp out of the string - this is important part - this is where you can take timezone into account (see difference in dates in the result 2018-05-29 vs. 2018-05-29). Then you can get Date out of TIMESTAMP
I want to use it in where clause with DATE Range between.
So, now you can use below in your WHERE clause
WHERE DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) BETWEEN date1 AND date2
Update
You can use below to avoid dealing with "wrong" format
PARSE_DATE('%F', SUBSTR(ts, 1, 10))
In case if you need to account for timezone - you can use below (which fix : to . before applying PARSE_TIMESTAMP)
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', FORMAT('%s.%s', SUBSTR(ts, 1, 19), SUBSTR(ts, 21, 8))))

If you want the date in the same timezone represented, then the simplest method is to use string operations and convert to a date:
select PARSE_DATE('%Y-%m-%d', SUBSTR('2018-05-30T12:56:27:487+0200', 1, 10))

Related

How can I convert a string quarter year to a timestamp in Databricks SQL

In Databricks SQL, how can I convert a date in string format "2021Q2" to a timestamp with the last day of that quarter?
select
to_timestamp(
last_day(
to_date(
(left('2021Q4',4)||'-'||int(right('2021Q4',1)*3))||'-'||'1')))
from
my_table
It is a pity that Q is not working in formatting from string to date object (it is working only in reverse) - parsing Q with to_date(date, "YYYY'QQ") sadly will not work.
According to https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html:
Symbols of ‘E’, ‘F’, ‘q’ and ‘Q’ can only be used for datetime formatting, e.g. date_format. They are not allowed used for datetime parsing, e.g. to_timestamp.
Because of that we have to separate quarter and multiple by 4. Than convert it to date object (parse_ and take last_date of the month:
SELECT
last_day(
to_date(
concat(left("2021Q4", 4), int(right("2021Q4", 1))*3),
"yyyyMM")
) as last_day_of_quarter
Simple way :
select to_timestamp(last_day(concat('2021','-',0,4*3,'-01'))) as last_date_queter
Logic :
calculate the last month of quarter by using multiple with 3. example 4th quarter's last month calculated 12 (4*3)
concat (year,'-',-01) so that we can get the first day of respective month 2021-12-01
last_day we can use last date of the given date month.
finally , we can convert the date into timestamp to_timestamp

How do I convert a date timestamp in oracle to a date?

I'm looking to convert (using Oracle) a date timestamp
[2018-01-25T00:00:00.000+00:00] to a date [2018-01-24]. I've tried several formats however I can't seem to find the right one to convert it. I'm unsure of how to handle the +00:00.
Thanks in advance
It depends on what you really ask.
It you have a real Oracle timestamp and you want a string in format 'YYYY-MM-DD', you can use to_char():
select to_char(col, 'YYYY-MM-DD') as res from mytable
If you have a string in ISO timestamp format, and you want a string as a result:
select substr(col, 1, 10) as res from mytable
If you have a timestamp column and you want to set the time portion to 00:00:00:
select trunc(col) as res from mytable;
This returns a value of datatype date.

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool.
For example if i want data of june month ? how can i fetch ?
You can use the TO_DATE(…) function to treat the VARCHAR column as a formatted date type, and the EXTRACT(…) function to retrieve just the month out of the date.
If your date string is formatted in a well-known manner, TO_DATE's automatic parsing (or a direct cast using the :: operator) will suffice, and you can write your query this way:
SELECT * FROM table
WHERE
EXTRACT(month, TO_DATE(varcharCol)) = 6 -- June of every year
AND EXTRACT(year, varcharCol::DATE) = 2020; -- June of 2020 alone
Alternatively, if the date is in a non-standard format, use available formatting options to make TO_DATE(…) parse it properly:
-- Dates of custom format, such as: 'June # 02 # 2020'
SELECT
EXTRACT(month, TO_DATE(varcharCol, 'MMMM # DD # YYYY')) AS month
FROM table
WHERE
month = 6;
Note: You can also swap all DATE and TO_DATE above with TIMESTAMP and TO_TIMESTAMP if the data carries a whole timestamp value within it instead of only a date.
First of all, you shouldn't store dates as strings. But you probably know that.
If you do store dates as strings, you store them all in one particular format, say, 'mm/dd/yyyy'. So, use a substring function to get the month digits.
For 'mm/dd/yyyy':
where substring(date_string, 1, 2) = '06'
For 'yyyy-mm-dd':
where substring(date_string, 9, 2) = '06'
In many situations you can also use LIKE:
For 'mm/dd/yyyy':
where date_string like '06%'
For 'yyyy-mm-dd':
where date_string like '%-06-%'
You have to use to_date in snowflake to convert varchar datatype to date as following
select *
from yourTable
where to_date(yourDateColumn, 'YYYY-MM-DD') >= '2020-06-01'
and to_date(yourDateColumn, 'YYYY-MM-DD') <= '2020-06-30'

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'

Invalid date error when modifying Big Query Table schema from string (YYYY-MM-DD HH:MM:SS TIMEZONE) to date

I have a table with a string format column where value are like this :
YYYY-MM-DD HH:MM:SS TIMEZONE so for example 2015-08-27 19:42:53 UTC
UTC is the only timezone. I want to rewrite this string column as a date column. I have an Invalid date error when I'm trying to run the following query to export the result in a new table
SELECT
CAST(my_date AS DATE),stuff_here,stuff_here
FROM
`table`
What should I do in order to properly change the type of this column from string to date ?
You appear to be trying to write this logic:
select CAST(substr('2015-08-27 19:42:53 UTC', 1, 10) AS DATE)
Because I am in New York, I would instead write:
select date(timestamp('2015-08-27 19:42:53 UTC'), 'America/New_York')
This distinction has been very important in our using data -- the difference between days at the Greenwich Meridian versus in our local area.
Try:
SELECT CAST(SUBSTR(my_date,1,19) AS DATE,stuff_here,stuff_here
FROM `table`
This assumes that you are not interested in the timezone part.