BigQuery - Convert date string into timestamp - google-bigquery

I am streaming inserts into bigquery from adwords javascript. The javascript time stamp that gets inserted into the bigquery table as a string - "Sun Sep 06 2015 02:42:54 GMT-0700 (PDT)". How do i convert this string into a timestamp of format "2015-09-06 02:42:54" in a bigquery select statement.

You can do this by parsing out each part of the date (year, month, day, etc.) using the REGEXP_EXTRACT function, then constructing a string to pass into the TIMESTAMP function. You can check out the query reference for more info on these functions. See also: a similar question with a different initial date format.
Note however that BigQuery TIMESTAMPs are stored as UTC times, so you may want to shift the time according to the timezone in the string.

Related

Convert "Fri 12/25/20" to date 2020-12-25 in bigquery

I uploaded a google sheet into BQ and the date format is "DOW mm/dd/yyyy" and of course BQ recognizes it as a string. I want to convert it to date without the DOW. How can I do this without changing the date format in google sheets (which is not an option)?
use below
select parse_date('%a %m/%d/%y', 'Fri 12/25/20')
with output
You just need to use the CAST as DATE function with the appropriate format string

What is expected input date pattern for date_format function in databricks spark SQL

I am trying to better understand the date_format function offered by Spark SQL.As per the official databricks documentation (I am using databricks), this function expects any date/ string in a valid datetime format. Below is the link for the same.
I am finding it difficult to understand what is the exact definition of "valid" here. I am trying to understand the functionality through two examples here.
Input string in YYYY-MM-DD format (2021-07-09), for which I get the expected results correctly:
Input string in DD-MM-YYYY format (20-07-2021), and I get null:
Why is this happening? How did this function understand that the parameter that I am passing is indeed in YYYY-MM-DD format? It could also have been YYYY-DD-MM.
My requirement is that I implement a logic that could handle all kinds of valid date formats (MM-DD-YYYY, YYYY-MM-DD, DD-MM-YYYY) and format the dates accordingly.
The following is valid input and output formats for ANSI date/time data types:
Example: ANSIDATE yyyy-mm-dd 2007-02-28 TIME WITH TIME ZONE hh:mm:ss.ffff... [+|-]th:tm
The valid range of time zone offset is from -14:00 to +14:00. date complies with the ANSI SQL standard definition for the Gregorian calendar: "NOTE 85 - Datetime data types will allow dates in the Gregorian format to be stored in the date range 0001-01-01 CE through 9999-12-31 CE
See Databricks SQL datetime patterns for details on valid formats. The function checks that the resulting dates are valid dates in the Proleptic Gregorian calendar, otherwise it returns NULL
When you use "20-07-2021" it does not conform to "yyyy-mm-dd" so results in NULL
Alternately, you can use make_date function which Creates a date from year, month, and day fields. Or better use to_date function
select date_format(to_date('9/15/2021', 'MM/dd/yyyy'), 'yyyy/MM/dd')
See Datetime Patterns for Formatting and Parsing in Spark.

Converting timestamp on whole table in bigquery

I have this table which stores millions of rows of data. This data has a date that indicates when was the data entered. I store the data in NUMERIC schemas with EPOCH UNIX as the format. However, I wanted to convert them to human date (yyyy-mm-dd hh:mm:ss) and later sort them by date not queried date.
However, it took me so long to find a suitable way. Here's my attempt.
I used SELECT CAST(DATE(timestamp) AS DATE) AS CURR_DT FROM dataset.table but it gave me this error:
No matching signature for function DATE for argument types: NUMERIC. Supported signatures: DATE(TIMESTAMP, [STRING]); DATE(DATETIME); DATE(INT64, INT64, INT64) at [1:13]
I used this method BigQuery: convert epoch to TIMESTAMP but still didn't fully understand
I'm a novice in coding so I hope you guys understand the situation. Thanks!
If I am understanding your question correctly you would like to take a numeric EPOCH time that is stored as an integer and convert it to a timestamp?
If so you can use the following in BigQuery Standard SQL:
select TIMESTAMP_SECONDS(1606048220)
It gives the output of:
2020-11-22 12:30:20 UTC
Documentation
If you only want the date component, then you would convert to a date after converting to a timestamp. Presumably you have seconds, so you would use TIMESTAMP_SECONDS() -- but there are similar functions for milliseconds and microseconds.
For just the date:
select date(timestamp_seconds(col))
Note that this removes the time component.

Convert date format in Oracle

I have a date format 2011-01-06T06:30:10Z in Excel.I want to just load the date part into a table from excel.How do I get the date part from it.
i.e. 2011-01-06
Thanks
Try this:
select cast(TO_TIMESTAMP_TZ(REPLACE('2011-01-06T06:30:10Z', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') as date) from dual
I think, some more explanation is needed.
Loading data into database is one part, and displaying it after fetching is another part.
If you have loaded the data into database, then all you need to do is use TRUNC. It will truncate the time portion and will display only the date portion.
A DATE always has a datetime part together. TIMESTAMP is an extension to the DATE type. And what you see the date looks like is not the way it is stored in database. The format is for we human beings to understand. A date is stored in 7 byte in internal format.
More information Based on OP's question via comments
NEVER store a DATE as VARCHAR2 datatype. A date is not a string literal. Oracle provides lot of FORMAT MODELS to display the datetime the way you want. Sooner or later, you will run into performance issues due to data conversion. Always use explicit conversion to convert a literal to a perfect DATE to compare it with other date value.

SQLite order by date

My date is not in standard form. This is how it is: 07/15/2013 06:53:05 and is stored as string in the database.
How can I query it to order it by date.
This query is not working.
SELECT jobno, ondate FROM Reports ORDER BY DATE(ondate)
When I run this query it orders it alphabetically and not date wise.
As written here, SQLite doesn't have a date type, so you can do this:
SELECT jobno, ondate
FROM Reports
ORDER BY substr(ondate,7)||substr(ondate,1,2)||substr(ondate,4,2)
sql fiddle demo
This is why this format is not a recommend date/time storage mechanism in SQLite.
You can use strftime to covert the data into something that can be well-ordered (note that we just have to specify the American-ish format string), e.g.
SELECT jobno, ondate FROM Reports
ORDER BY strftime('%m/%d/%Y %H:%M:%S', ondate)
However, this approach (as well as the approach in the other answer) will not be able to use indices and performance on large data may suffer!
From SQLite Datatypes: 1.2 Date and Time Datatype the advice is:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
I recommend using ISO 8601 as it's still human-readable, is well-understood (and culture neutral), can encode the TZ (just be consistent!), and can be indexed well.