BigQuery SELECT NULL AS "some_name" : Unexpected String Literal - google-bigquery

I would like to set a string column as NULL in BigQuery with the query
SELECT NULL AS "h3-2"
FROM `sometable` LIMIT 10
But I get the error Syntax error: Unexpected string literal "h3-2" at [1:16]
How do I set a column as NULL?

The problem was indeed using a string literal.. I added the quotes because h3-2 also wasn't working but instead I had to do
SELECT NULL AS h3_2
FROM `sometable` LIMIT 10
to get it to work

Related

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,
....

Getting an error: Argument data type varchar is invalid for argument 2 of substring function

I'm trying to get a substring from the value of a column and I'm getting the following error Argument data type varchar is invalid for argument 2 of substring function.
The column type is NvarChar(50) and is a system column for an application, so I can't modify it.
Ideally I'd just be able to select the substring as part of the query without having to alter the table, or create a view or another table.
Here's my query
SELECT SUBSTRING(INVOICE__, ':', 1)
FROM dwsystem.dbo.DWGroup
Im trying to select only everything in the string after a specific character. In this case the : character.
Use charindex with : as the first argument
select substring(invoice__,charindex(':',invoice__)+1,len(invoice__))
from dwsystem.dbo.dwgroup
SUBSTRING parameter is start position and end position so both parameter will be number like below
SELECT SUBSTRING(INVOICE__, 1, 1)
FROM dwsystem.dbo.DWGroup
you can use SUBSTRING_INDEX as you used mysql
SELECT SUBSTRING_INDEX(INVOICE__,':',-1);
example
SELECT SUBSTRING_INDEX('mytestpage:info',':',-1); it will return
info

Standard Big query Not removing String

I'm trying to filter out "null" from a string column using Standard Big query and for whatever reason it is not being filtered out
so my where statement goes:
where d_transaction_dt <> "null"
d_transaction_dt is a string column that I'm trying to cast as date and remove anything in there that is "null"
I'm getting the error: Invalid date: 'null'
Please help..
Is it literally the string "null", or is the field null? If you're doing something like a SAFE_CAST to convert strings to a DATE type, you're probably getting NULL values.
WHERE d_transaction_dt <> "null"
is an entirely different filter predicate than
WHERE d_transaction_dt IS NOT NULL

hive sql transform big int array to string array

I am trying to convert an array of big ints into string array in hive SQL
I've tried using
concat_ws
but that gave me an error
Argument 2 of function CONCAT_WS must be "string or array<string>", but "array<bigint>" was found.
I tried using
transform(mycolumn) using '/bin/cat' as mycolumn_as_string
It gives an error of
cannot recognize input near 'transform' '(' 'mycolumn' in selection target
How can I convert this bigint array into an array string?
Assuming that your table looks like this:
tb_name mycolumn
------- --------
tblname [111,222,333]
tblname [1234,123456,3423]
Then below query will not work because concat_ws only accepts string or array of string:
select tb_name, concat_ws('-', mycolumn) from mytable;
FAILED: Argument 2 of function CONCAT_WS must be "string or array<string>", but "array<int>" was found.
Based on this SO: How to concatenate the elemets of int array to string in Hive
Incorrect:
select concat_ws('-', transform(mycolumn) using '/bin/cat') as mycolumnstr from mytable;
FAILED: ParseException line 1:22 cannot recognize input near 'transform' '(' 'mycolumn' in select expression
Correct:
with tbl as (select transform(mycolumn, tb_name) using '/bin/cat' as (mycolumnstr, tb_name) from mytable)
select concat_ws('-', tb_name, mycolumnstr) from tbl;
Result:
tblname-[111,222,333]
tblname-[1234,123456,3423]

ORA-01722: invalid number error

I am running the below mentioned query in my Oracle client and i am getting
ORA-01722: invalid number
error. I know the issue is due to the TAG_VALUE column being of type "varchar2" and i am converting it to number and then using that field in where clause. I have tried using "CAST" function but that is also not helping.
If i run the query neglecting the last where condition with code WHERE (P.TAG_VALUE > '100') then i am getting the result but including the last where clause gives me error.
SELECT DISTINCT
count(P.CREATED_DATETIME)
FROM
(
select OUTPUT_TAG_ID,TO_NUMBER(TAG_VAL,'9999.99') AS
TAG_VALUE,TAG_VAL_TS,CREATED_DATETIME
from OV80STG.PRCSD_DATA_OUTPUT_ARCHIVE
where MODEL_CODE='MDLADV1538'
AND TAG_VAL <> 'U_Transfer_rate'
) P
WHERE
(P.TAG_VALUE > '100')
Any suggestion will be appreciated. Thanks.
Remove the single quotes from around the value in the where, you don't need them when its an integer. query will be like this:
SELECT DISTINCT
COUNT(P.CREATED_DATETIME)
FROM
(
SELECT
OUTPUT_TAG_ID,
TO_NUMBER(TAG_VAL, '9999.99') AS TAG_VALUE,
TAG_VAL_TS,
CREATED_DATETIME
FROM OV80STG.PRCSD_DATA_OUTPUT_ARCHIVE
WHERE MODEL_CODE = 'MDLADV1538'
AND TAG_VAL <> 'U_Transfer_rate'
) P
WHERE(P.TAG_VALUE > 100);
TO_NUMBER function returns a numeric value so, as mentioned in comment, you shouldn't compare it with string value.
I solved the issue by including outer where clause inside the subquery and then I got the required result without any error.