Is there a way in BigQuery to convert a hex string to a decimal value?
Something like:
select hex("ff")
CAST now supports converting hexadecimal strings to INT64 or FLOAT64 values, even though it's not specified in their reference
Here's how you use it:
SELECT
CAST(columnA as FLOAT64) as float,
CAST(columnB as INT64) as int
FROM table
This should work, but it doesn't (I'm filing a feature request):
SELECT INTEGER('0xffff')
In the meantime, this does work:
SELECT FLOAT('0xffff')
255.0
For integer results:
SELECT INTEGER(FLOAT('0xffff'))
255
Looking into the query reference, I'd say no.
You have "HEX_STRING()" which does the opposite, but all the string to number functions seem to not take hex.
Related
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6
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)
I would like is possible to divide the column A by the column B as column C, in Bigquery.
For example SELECT prix / surface as prixmcarre FROM 'appartement', is not possible in bigquery.
Error: No matching signature for operator / for argument types: STRING, STRING. Supported signatures: FLOAT64 / FLOAT64; NUMERIC / NUMERIC at [1:8]
In the Google Cloud Document, I don't see the simple solution.
Thank's for your help :)
You must be using strings that look like integers or floats. You need to cast them. For example:
select cast('10' as INT64) / cast('5' as INT64)
I have a integer array represented with String. For example,
"[1,2,2,3]"
And the field type in Hive table is the array integer, I was wondering if there is any Hive build-in UDF that can cast the above string into the array integer.
Thanks
tl;dr I do not know of a Hive UDF that will do this for you, and casting on your own can be gross.
No, there isn't a UDF. As for building your own solution:
Casting to array[string] would be easy enough - just drop the square brackets using regexp_replace and split the resulting string on ,.
The problem is converting an array[string] to array[int] for arrays of arbitrary size. You can individually cast the array elements one by one:
hive> select id, my_array from array_table limit 3;
OK
10023307 ["0.20296966","0.17753501","-0.03543373"]
100308007 ["0.16155224","0.1945944","0.09167781"]
100384207 ["0.025892768","0.023214806","-0.003712816"]
hive> select array(cast(my_array[0] as double), cast(my_array[1] as double), cast(my_array[2] as double)) from array_table limit 3;
OK
[0.20296966,0.17753501,-0.03543373]
[0.16155224,0.1945944,0.09167781]
[0.025892768,0.023214806,-0.003712816]
but this approach only works because I know I have arrays of length 3.
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6