Create a view in bigquery with date rounded down to midnight? - sql

I am creating a view in BigQuery with the following SQL statement:
SELECT Id, CreatedDate FROM [bucketname.tablename]
How can I modify this query so that the CreatedDate becomes rounded down to midnight in the view?

in BigQuery legacy SQL: try TIMESTAMP(DATE(CreatedDate))
as an example
CreatedDate = '2016-04-30 07:01:28 UTC'
DATE(CreatedDate) = '2016-04-30'
and finally TIMESTAMP(DATE(CreatedDate)) = '2016-04-30 00:00:00 UTC'
In mode of standart SQL: try Format functions
FORMAT_TIMESTAMP("%Y-%m-%d 00:00:00", CURRENT_TIMESTAMP())

I believe you can just use the DATE function:
Returns a human-readable string of a TIMESTAMP data type in the format %Y-%m-%d.
So:
SELECT Id, DATE(CreatedDate) AS CreatedDate From [dataset.tablename]

Related

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'

BigQuery TIMESTAMP TO DATETIME

Conversion rules in Google say that TIMESTAMPS can become DATETIME.
The following code works to turn a TIMESTAMP to a DATE.
SELECT CAST( DATE(CURRENT_TIMESTAMP()) AS DATE)
We can get a DATETIME using this in Standard SQL but we lose the time in the process:
SELECT CAST ( CAST( DATE(CURRENT_TIMESTAMP()) AS DATE) AS DATETIME )
How do we get a TIMESTAMP to become a DATETIME without losing the time (i.e. something like this)?
SELECT CAST( CURRENT_TIMESTAMP() AS DATETIME )
How do we do this in Legacy SQL and Standard SQL on BigQuery?
NOTE: We just discovered our example CAST works on Standard SQL (it appears our query system was using Legacy SQL).
You can simply use CAST(... as DATETIME) as in below example
#standardSQL
WITH `project.dataset.table` AS (
SELECT CURRENT_TIMESTAMP() ts_as_timestamp
)
SELECT ts_as_timestamp, CAST(ts_as_timestamp AS DATETIME) ts_as_datetime
FROM `project.dataset.table`
with result
Row ts_as_timestamp ts_as_datetime
1 2019-06-13 19:22:42.839108 UTC 2019-06-13T19:22:42.839108
For Legacy SQL
#legacySQL
SELECT ts_as_timestamp, CAST(STRING(ts_as_timestamp) AS DATETIME) ts_as_datetime
FROM (SELECT CURRENT_TIMESTAMP() ts_as_timestamp)
with same output

Convert DATETIME format to custom format in bigquery

I have a column in BigQuery in DATETIME eg 2018-08-16T11:00:35.683000 which I would like to convert in some custom format which is YYYY-MM-dd HH:MM.
How can I do this ?
I am getting below error while converting:
ValueError: timestamp out of range for platform localtime()/gmtime() function
Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT DATETIME '2018-08-16T11:00:35.683000' dt
)
SELECT FORMAT_DATETIME('%Y-%m-%d %R', dt) cust_dt
FROM `project.dataset.table`
with result
Row cust_dt
1 2018-08-16 11:00

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.

Check only date from datetime field

I have a MySql database that holds datetime in one field, in this format:
'2011-01-04 16:32:49'
How can I filter out results based on this datetime such that it shows results only from Today. basically, I only want to compare against the date part of the field.
Can I do something like this?
Select * from table where timestamp.date() = now().date
Use DATE:
SELECT * FROM table
WHERE DATE(timestamp) = DATE(now())
Another alternative that will be able to use the index on timestamp if you have one:
SELECT * FROM table
WHERE timestamp >= DATE(now())
AND timestamp < DATE(now()) + interval 1 day
Yes you can but the column that stores the dateTime field in your database simply needs to be referenced, you should not be calling a .date() function on it.
For mySQL, this would be:
Select * from table where DATEDIFF(NOW(),timestamp) = 0
This would give you only records in which the Date is today.
Select * from table where DATE_FORMAT(timestamp, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')