Convert query with STR_TO_DATE to BigQuery syntax [duplicate] - mysql

This question already has answers here:
STRING to DATE in BIGQUERY
(4 answers)
Closed 4 years ago.
I have query that uses the following:
STR_TO_DATE(JSON_EXTRACT_SCALAR(fb.p_dataforanalytics,'$.birthday'), '%m/%d/%Y'),
STR_TO_DATE(JSON_EXTRACT_SCALAR(g.p_dataforanalytics,'$.birthday'), '%Y-%m-%d'),
date_format(p_birthday, '%Y-%m-%d')
This syntax works in MySQL but it doesn't work in BigQuery.
I somehow needs to convert it using PARSE_DATE(?) but I can't find the proper syntax for this.
EDIT:
PARSE_DATE('%m/%d/%Y', JSON_EXTRACT_SCALAR(fb.p_dataforanalytics,'$.birthday') ),
PARSE_DATE('%Y-%m-%d', JSON_EXTRACT_SCALAR(g.p_dataforanalytics,'$.birthday')),
PARSE_DATE('%Y-%m-%d', p_birthday)
Dosent work.
I get:
Invalid result from parsing function
and also
on the p_birthday row:
No matching signature for function PARSE_DATE for argument types:
STRING, INT64. Supported signature: PARSE_DATE(STRING, STRING) at
[15:33]

Easy one, with standard SQL:
#standardSQL
SELECT PARSE_DATE('%m/%d/%Y', '6/22/2017')
2017-06-22
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#supported-format-elements-for-date
JSON_EXTRACT_SCALAR should work unchanged.

Related

'OPENJSON' is not a recognized built-in function name [duplicate]

This question already has answers here:
Apply OPENJSON to a single column
(2 answers)
How to parse JSON column to a table with TSQL
(4 answers)
Closed 7 months ago.
I'm using Microsoft SQL Server 2019 - 15.0.4236.7 (X64)
Compatibility Level is 2019 \ 150
I have a very simple query to transpose some JSON that is stored in a column:
SELECT OPENJSON ([fieldname])
FROM mytable
But I keep recieing the error:
'OPENJSON' is not a recognized built-in function name.
Do I need to enable something?

How to display string as date? [duplicate]

This question already has answers here:
How to convert a string to date in MySQL?
(5 answers)
Closed 5 years ago.
I have a column(string (8)) that contains data like 12032008 and I have to display it as 12/03/2008. What should I do in retrieve query?
In mysql first convert it to date object using STR_TO_DATE then to format use DATE_FORMAT
SELECT DATE_FORMAT(STR_TO_DATE('12032008','%d%m%Y'),'%d/%m/%Y')
But ideally you should not save the date or date time as strings in table it will make things complex for mysql and for you also
Demo
The most appropriate thing for you to do here would be to stop storing date information in text columns. Instead, put 12032008 into a bona fide datetime column. This will make your life easiest in the long run. If you can't do that in the short term, here are two options for you to consider.
The short way, just using base string functions:
SELECT
CONCAT(LEFT(col, 2), '/', SUBSTRING(col, 3, 2), '/', RIGHT(col, 4)) AS output
FROM yourTable;
The longer way, and probably the better long term solution, would be to first convert your date string into a date, then convert it back to a string using the formatting you want:
SELECT
DATE_FORMAT(STR_TO_DATE(col, '%d%m%Y'), '%d/%m/%Y') AS output
FROM yourTable;
Demo

MySQL invalid repetition count(s)' from regexp [duplicate]

This question already has answers here:
Got error 'invalid repetition count(s)' from regexp
(1 answer)
MySql Regex Expression test
(1 answer)
Closed 5 years ago.
I'm having an issue with a query I'm running on MySQL that contains some regex, here it is: SELECT * FROM dump WHERE Hours REGEXP '^\d{337}1{8}'
When I test my regex separately it's working fine, here's some sample data that it matches with:
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000000
Any idea where it fails?

vb.net IsDate doesnt work for invalid dates [duplicate]

This question already has an answer here:
Unable to convert MySQL date/time value to System.DateTime.Couldn't store 0/0/0000 0:00:00 value?
(1 answer)
Closed 7 years ago.
In my MySql database i have a date column, then in vb.net i am using this code:
If Not IsDate(reader3.GetDateTime(0).ToString("yyyy-MM-dd")) Then
End If
but if the date in the database is 0000-00-00 its returning an error:
Unable to convert MySQL date/time value to System.DateTime
i have also tried remove the ToString
If Not IsDate(reader3.GetDateTime(0)) Then
End If
but i still get the same error
A way is to use IF into your MySql Select:
IF(YourDateField='0000-00-00' OR YourDateField IS NULL,'',YourDateField) AS YourDateField
So, when the field is null or equal to '0000-00-00', the output from DB will be an empty string and you'll avoid vb.net errors.

STR_TO_DATE returns blob value [duplicate]

This question already has an answer here:
phpMyAdmin: Date Fields Display as BLOB
(1 answer)
Closed 7 years ago.
I don't understand why mysql returns a blob value here :
SELECT DAT_REAL, STR_TO_DATE(dat_real, '%d-%m-%Y') FROM `entretiens` where dat_real <> ''
from phpmyadmin :
clicking on the blob link leads me to a page saying request =
SELECT STR_TO_DATE(dat_real, '%d-%m-%Y') FROM 'entretiens' WHERE 'entretiens'.'DAT_REAL' = '02-09-2010' AND .'STR_TO_DATE(dat_real, '%d-%m-%Y')' = '2010-09-02';
type of DAT_REAL is varchar(12)
Please specify the data type of "dat_real" field. And also please ensure that it must be a string, since STR_TO_DATE accepts first parameter as a date in string format.