I have a string containing a latitude/longitude pair:
"50.0814253N, 14.4876106E"
I’d like to parse it into Maybe (Float, Float). I can easily get to [Just 50.0814253, Just 14.4876106] or something like that, but getting the final Maybe (Float, Float) is quite messy. Is there a nice way to get from List (Maybe Float) to Maybe (Float, Float)?
If you already have [Just 50.0814253, Just 14.4876106] and want Maybe (Float, Float), you can use simple pattern matching with case ... of:
let list = [Just 50.0814253, Just 14.4876106]
in
case list of
[Just a, Just b] ->
Just (a, b)
_ ->
None
Related
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)
This question may answer on many threads but I am unable to find answer specific to my problem.
Q: I am getting data from API (in json format) where all columns are coming as string and inserting into a table which has all columns as string and serving as source table.
Now, I am trying to cast data from that source to destination and making all necessary casting to insert data into destination table. But decimal (16,8) casting failed.
I debug issue at my end and found that during the fetching data from API which is returning the data into json format converting values in some unusual format.
For e.g. 0.00007 converting into 7E-05 and this is happening for many other rows.
I know I can fix this problem at API implementation level. But I asked to solve this at SQL server end. So I need a solution which should convert 7E-05 into 0.00007.
Try something like:
SELECT CAST(CAST(#t AS FLOAT) AS DECIMAL(16,8))
Results in:
0.00007000
CAST to a FLOAT, then to a DECIMAL.
This unusual format is a rather usual scientific notation Wikipedia, read section "E-notation"
You see the E and a number meaning exponent.
"1E2" = 1 * 10^2 = 100
"1E-2" = 1 * 10^(-2) = 0.01
Try this out:
DECLARE #tbl TABLE(Numberstring VARCHAR(100));
INSERT INTO #tbl VALUES('100'),('1E2'),('1E-2'),('7E-05');
SELECT Numberstring
,CAST(Numberstring AS FLOAT)
,CAST(CAST(Numberstring AS FLOAT) AS DECIMAL(20,10))
FROM #tbl;
The result
100 100 100.0000000000
1E2 100 100.0000000000
1E-2 0,01 0.0100000000
7E-05 7E-05 0.0000700000
You can see, that the FLOAT type itself will display the last one in the scientific notation, while the cast to DECIMAL will return the number you are expecting.
I'd be happy with an upvote, but you should accept Shawn's answer as it was earlier than mine :-D
I would like to query a hive table only for those rows that have coulmn1 as integer value only. Due to some data corruption, without this check I am getting a lot of junk data, I would like to get rid of that data by applying where column1 is INT kind of condition, but I couldn't find anything like that in hive. Could anyone suggest how I could do it?
Without any example data, I would suggest something very basic like this:
define column X as STRING
check that X = cast(cast(X as INT) as STRING)
You may have to add some tolerance to blank space, zero-padding, etc. depending on the way your "integers" are actually formatted.
Found a solution that is working :
I could add a Double number check like below, anything other than just numbers will make it null. Also, the valid numbers for the column will never cross the Double range.
So we could do something like below I guess:
"select * from table_example where cast(column1 as double) is not null"
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.
For example I have a column that contains string (in English - "A", "B" ) "AB1234" and I'd like to compare it to the string "AB1234" (in Russian "A", "B" ), for Example.
Is there any built-in function to achieve this?
The best way I found is to use Translate func where i enumerate all needed symbols.
You are looking for a function LOOKS LIKE.
Unfortunately there is no such function in SQL.
Instead, you can create a function-based index that casts every string to a common denominator using TRANSLATE, and search for the string:
CREATE INDEX ix_mytable_transliterate ON (TRANSLATE(UPPER(str), 'АВЕКМНОРСТУХ', 'AВEKMHOPCTYX'))
SELECT *
FROM mytable
WHERE TRANSLATE(UPPER(str), 'АВЕКМНОРСТУХ', 'AВEKMHOPCTYX') = TRANSLATE(UPPER('весна на танке'), 'АВЕКМНОРСТУХ', 'AВEKMHOPCTYX')