convert string dd-mm-yyyy to date yyyy-mm-dd in bigquery - sql

I have 600 string fields in a table with format eg.,18.05.2015 and i want to convert into date 2015-05-18 in bigquery. I have tried using timestamp() and date() function but it is returning null values

In Standard SQL
SELECT PARSE_DATE('%d.%m.%Y', '18.05.2015')
the query against table will look like
SELECT PARSE_DATE('%d.%m.%Y', YourDateColumn)
FROM `YourDataset.YourTable`
Added to address 'broken' values
WITH YourTable AS (
SELECT '18.05.2015' AS dt UNION ALL
SELECT '#' AS dt
)
SELECT
CASE WHEN REGEXP_CONTAINS(dt, r'\d{2}\.\d{2}\.\d{4}')
THEN CAST(PARSE_DATE('%d.%m.%Y', dt) AS STRING)
ELSE dt
END AS new_dt
FROM YourTable
what this does is - process only values that match 18.05.2015 format and leaves any other untouched
I have multiple date columns with 600 records
Making FINAL attempt to interpret your comments - but honestly, still feel like it is not what you have and you are not giving clear picture, so it is best i could make for you!
CREATE TEMPORARY FUNCTION FIX(x STRING)
RETURNS STRING AS (
CASE WHEN REGEXP_CONTAINS(x, r'\d{2}\.\d{2}\.\d{4}')
THEN CAST(PARSE_DATE('%d.%m.%Y', x) AS STRING) ELSE x END);
WITH YourTable AS (
SELECT '18.05.2015' AS dt_001, '19.05.2015' AS dt_002, '21.05.2015' AS dt_003 UNION ALL
SELECT '#' AS dt_001, '20.05.2015' AS dt_002, 'abc' AS dt_003
)
SELECT
FIX(dt_001) AS new_dt_001,
FIX(dt_002) AS new_dt_002,
FIX(dt_003) AS new_dt_003
FROM YourTable

you can update your all the string fields from dd.mm.yyy to yyyy-mm-dd format using following query.
update TABLE_NAME
set FIELD_NAME = concat(SUBSTRING(FIELD_NAME,-4),'-',SUBSTRING(FIELD_NAME,-7,2),'-',SUBSTRING(FIELD_NAME,1,2))

Related

Get all rows with a list of date between two date columns in SQL

I have a table named tableA which has two date columns. Currently, I am using the below query to fetch data.
"select * from tableA where IN_Date between date1 and date2"
IN_DATE is input param from the proc
Now instead of one date IN_DATE, I want to pass a list of dates but I am not sure how to update the query. Please help.
TableA
id date1 date2
The solution to your problem
select * from tableA where (
(IN_Date between date1 and date2) or
(IN_Date between date3 and date4) or
(IN_Date between date5 and date6)
)
What you are trying to do simply is not possible.
The syntax of the between clause is:
... expression1 BETWEEN expression2 AND expression3 ...
Each expression must resolve to a single value (not a list of values). Furthermore expression2 be < expression3, otherwise results are undefined.
Where expressionN is a column name, then the single value is the value in the row currently being evaluated.
This suggests that you may be approaching this incorrectly. Please provide some sample data, and expected results. This will allow a better understanding of what you are trying to do. A description of what you are wanting to achieve would also be helpful, rather than a description of how you are trying to achieve it.
You may use a string tokenization approach like below, where the IN_DATE string parameter has comma separated list of dates in the form of YYYY-MM-DD.
select *
from tableA t
where exists
(
select 1
from xmltable
(
'for $id in tokenize($s, ",") return <i>{normalize-space ($id)}</i>'
passing IN_DATE as "s"
columns
tok char(10) path '.'
) v
where date (to_date (v.tok, 'YYYY-MM-DD')) between t.date1 and t.date2
)

BigQuery SQL: Given string input. how to confirm it is a valid date

I am working on filter the records that don't have valid dates associated with them.
The expectant is a string of format yyyymmddhhmmss . How can I validate that the string is actually a valid date?
For example the input string could be: 202101....## (invalid literals)
Would a filter like ...
WHERE SAFE_CAST (datestring AS DATE) IS NOT NULL
serve your purpose?
You can use safe.parse_datetime('%Y%m%d%H%M%S', col_string) as col_datetime - if col_string represents valid datetime - it will output that valid datetime, otherwise - null, as in dummy example below
with your_table as (
select '20211215031521' col_string union all
select '20211215031521Y' union all
select '202112150H1521'
)
select col_string,
safe.parse_datetime('%Y%m%d%H%M%S', col_string) as col_datetime
from your_table
with output

Date column which contains null values as well

i have column called startup_date which defined as STRING datatype in bigquery
which contains value like "2001-09-09 02:19:38.0 UTC" and null values as well
please help to use convert function to fetch only date value not hours and mins
used below function and getting invalid datetime string error message
EXTRACT(date FROM
datetime(CASE when startup_date = '' THEN NULL ELSE startup_date END))
The DATE and TIMESTAMP functions do exactly what you are looking for. If you have a STRING column where its format is like TIMESTAMP, you can simply apply it. Then, DATE will extract just the date and it takes care of the NULL values.
WITH my_data AS
(
SELECT TIMESTAMP("2001-09-09 02:19:38.0 UTC") AS startup_date UNION ALL
SELECT NULL UNION ALL
SELECT "2021-10-10 07:29:30.0 UTC"
)
SELECT DATE(startup_date) as date FROM my_data
returns:
You can try substr[1] from 1 to 10 to get the date, and then you can use the safe.parse_date function[2].
SELECT safe.parse_date('%Y-%m-%d', substr(startup_date, 1, 10)) AS startup_date FROM you_dataset.your_table
It returns this:
[1] https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#substr
[2] https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#parse_date

Invalid datetime string when CAST As Date

I have Time column in BigQuery, the values of which look like this: 2020-09-01-07:53:19 it is a STRING format. I need to extract just the date. Desired output: 2020-09-01.
My query:
SELECT
CAST(a.Time AS date) as Date
from `table_a`
The error message is: Invalid datetime string "2020-09-02-02:17:49"
You could also use the parse_datetime(), then convert to a date.
with temp as (select '2020-09-02-02:17:49' as Time)
select
date(parse_datetime('%Y-%m-%d-%T',Time)) as new_date
from temp
How about just taking the left-most 10 characters?
select substr(a.time, 1, 10)
If you want this as a date, then:
select parse_date('%Y-%m-%d', substr(a.time, 1, 10))
select STR_TO_DATE('2020-09-08 00:58:09','%Y-%m-%d') from DUAL;
or to be more specific as your column do as:
select STR_TO_DATE(a.Time,'%Y-%m-%d') from `table_a`;
Note: this format is applicable where mysql is supported

SQL Server: how to add case statement to select

I am using the following select to query a date from a database table.
The input (ms) for this query results from an xml string and the stored procedure then loops through all the single values in the xml to return a certain number (integer) for each of them.
This works fine so far.
Is there a way that I can return a placeholder number (like 99999) if the input (ms) is empty / nothing ?
Currently the below returns 0 in such a case which I cannot use to identify this as 0 can also be a valid result in other cases.
My stored procedure so far:
SELECT ms as date,
type,
(
SELECT COUNT(calendar_dt)
FROM Calendar
WHERE day_of_week NOT IN (1, 7)
AND calendar_dt > GETDATE()
AND calendar_dt <= ms
) as bDays
FROM #dates
FOR XML PATH('ms'), ELEMENTS, TYPE, ROOT('ranks')
Many thanks in advance for any help with this, Tim.
If the column "ms" is actually NULL or populated, just use ISNULL.
http://technet.microsoft.com/en-us/library/ms184325.aspx
SELECT ISNULL(ms, 99999) AS date
However, if that column can contain an empty string, which is not the same as NULL, then also use NULLIF.
http://technet.microsoft.com/en-us/library/ms177562.aspx
SELECT ISNULL(NULLIF(ms,''), 99999) AS date