Bigquery converting string to timestamp - google-bigquery

Whats the best way to convert a string value like this to a timestamp?
"2021-05-11T02:00:03.4897671"
I tried the below
select PARSE_TIMESTAMP("%Y-%m-%d %H:%M:%E#S", "2021-05-11T02:00:03.4897671")
But I get the following error
Failed to parse input string "2021-05-11T02:00:03.4897671"

Try this:
select PARSE_TIMESTAMP("%Y-%m-%dT%H:%M:%E*S", "2021-05-11T02:00:03.4897671")

Related

Get the first value of a json

I have a json like this in an Athena table:
input
[14587979, {ke1:4,ke2:4}]
I would like to get the fist value "14587979"
but when I use json_extract like this:
json_extract(input, "$[0]") as id
I get the below error
Query execution failed
Reason:
SQL Error [100071] [HY000]: [Simba]AthenaJDBC An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 3:22: Column '$[0]' cannot be resolved
how Can I get the first value?
Use single quotes, double quotes are used to escape column/table/schema names (if for some example the name contains restricted character like space):
select json_extract('[14587979, {ke1:4,ke2:4}]', '$[0]');
Output:
_col0
14587979

Bad int value BigQuery

I have the following column of strings like:
1635188264984-384-2141356
And I'd like it as an integer type
16351882649843842141356
But when I use the following query:
cast(REPLACE(my_table.my_column,'-','') as int)
I get the following error in BigQuery:
Bad int64 value: 16351882649843842141356
What am I doing wrong and how can I accomplish converting string such as this to integers?
Range for INT is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (see more at https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#integer_type)
So, use NUMERIC instead as in below example
cast(REPLACE(my_table.my_column,'-','') as numeric)

Getting: INVALID_FUNCTION_ARGUMENT: Invalid format: "" when trying to date_parse a string formatted date

Query:
SELECT date_parse(start_date, '%Y/%m/%d')
FROM sql_question_five_2 ;
date format looks like this in csv: 20210531
being read into table as string.
Have tried a few different things to get it to convert to a date YYYY-MM-DD
date_parse expects all inputs to look like the format string, if they don't it will throw an error. You can do something like this to avoid the problem:
SELECT IF(start_date = '', NULL, date_parse(start_date, '%Y/%m/%d'))
FROM sql_question_five_2
This guards against the case where the string is empty, which is the case when you get the error. If you have other strings that don't conform to the format you would have to guard against those too.
If that is the case you can use the TRY function which captures errors and returns NULL:
SELECT TRY(date_parse(start_date, '%Y/%m/%d'))
FROM sql_question_five_2
Your format string needs to match the source value. Your source doesn’t have /‘s in it so your format string shouldn’t either

Using try_cast in snowflake to deal with very long numbers

I'm using try_cast in snowflake to convert any long values in sql to NULL.
Here is my code:
When I try running the above code, I'm getting the error as below:
I'm flattening a JSON array and using try_cast to make any large values to NULL because I was getting an error Failed to cast variant value {numberLong: -8301085358432}
SELECT try_cast(item.value:price) as item_price,
try_cast(item.value:total_price_bill) as items_total_price
FROM table, LATERAL FLATTEN(input => products) item
Error:
SQL compilation error error at line 1 at position ')'.
I don't understand where I'm doing wrong
you are using wrong syntax for try_cast. according to snowflake documentations the syntax is :
TRY_CAST( <source_string_expr> AS <target_data_type> )
and also note:
Only works for string expressions.
target_data_type must be one of the following:
VARCHAR (or any of its synonyms)
NUMBER (or any of its synonyms)
DOUBLE
BOOLEAN
DATE
TIME
TIMESTAMP, TIMESTAMP_LTZ, TIMESTAMP_NTZ, or TIMESTAMP_TZ
so for example you have to have something like this if item.value:price is string:
select try_cast(item.value:price as NUMBER) as item_price,
....

format of the 'TEXT' in OCINumberFromText in C code

I want to insert values like B194YV into an array and ultimately to OCINumber to use in Oracle statement.
I tried to insert like this
rc = OCINumberFromText(errhp, (text*)new_value, strlen(new_value),
(text*)"99G999D99", 9, (text *)"NLS_NUMERIC_CHARACTERS='.''", 27, &num_val);
Here new_value has value B194YV, but it doesnt work. i get error
OCI-22062: invalid input string [B194YV]
I know 99G999D99 is not the correct format, so can anyone please tell me what format should i write?
Thank you
You are trying to convert a string "B194YV" to a number. This is simply not possible. It's not a number.