Tableau CustomSQL Cast NUMERIC to FLOAT - sql

I am writing a customSQL in Tableau to import data from BigQuery.
Columns in BigQuery View
Column1 | VALUE(Type = Numeric)
I want to CAST the "VALUE" column to Float, so that the column appears in Tableau
My current SQL query:
SELECT Column1, CAST(VALUE as Float) from Table 1
The above statement is giving me an error. Any idea?

According to this information on the BigQuery site the data type should be FLOAT64.
CAST(values AS FLOAT64)
(Original Answer pre-comment)
You are able to convert it to a float in Tableau. FLOAT([YourField])

Finally got it to work using the below SQL
SELECT Column1, CAST(VALUE as Float64) AS VALUE from Table 1.
I was missing "AS VALUE" in my previous SQLstatement.
Note: Tableau v2020.3 will support Numeric data type

Related

Convert string columns into numeric columns with SQL in Google BigQuery

I'm analyzing the data of New York City taxi trips of yellow cars in 2018. (You need a Google BigQuery account to access this data set.)
The schema says that most of the columns are numeric. However, when I tried to calculate the sum of the key dollar figures (tip_amount, tolls_amount, total_amount), I got an error message saying that they are string variables.
SELECT sum(total_amount)
FROM [bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
WHERE month(dropoff_datetime) = 12
Error: Field total_amount is of type STRING which is not supported for SUM
I then tried to use the cast() function to convert it to a numeric variable, but that did not work.
SELECT sum(total_amount_numeric) FROM
(
SELECT cast(total_amount as numeric) as total_amount_numeric
FROM [bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
WHERE month(dropoff_datetime) = 12
)
Error: Field total_amount_numeric is of type STRING which is not supported for SUM
How can I analyze these numeric variables as I intended, instead of the string variables as they are erroneously set in the database?
Below is for BigQuery Standard SQL
#standardSQL
SELECT SUM(total_amount)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
WHERE EXTRACT(MONTH FROM dropoff_datetime) = 12
The problem you had is because NUMERIC data type is not supported by BigQuery Legacy SQL and rather is treated as STRING and cannot CAST to neither FLOAT nor INTEGER
So, the workaround is to use BigQuery Standard SQL as in above example - and as you see here you don't need to do any CAST'ing as this field is already NUMERIC
Your query will run as follows in Standard SQL:
SELECT sum(total_amount_numeric)
FROM (SELECT cast(total_amount as numeric) as total_amount_numeric
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
WHERE EXTRACT(month FROM dropoff_datetime) = 12
) x;
You can include this hint before the query to ensure that it is run using standard SQL:
#standardSQL

BigQuery Help - How to cast and convert to float and date format

I'm trying to do two things in BigQuery, but I'm having difficulties doing so.
I'd like to do two things :
Convert my date columns to Date format (it is currently in int64, with 43379 as an example)
Cast my columns Delivered_Cost and Actual_Cost to float (they are currently with string type) - As when there is a null value, there is - instead of 0. When you cast to float, do these -s automatically change to 0 or do I have to update that first?
I don't have much experience this, and I've been having difficulties looking for a solution online so I'd love any help! I'm having difficulties casting and displaying data from my table at the same time.
Thank you!
SELECT * FROM TABLE1
CAST(Delivered_Cost as float)
Convert my date columns to Date format (it is currently in int64, with 43379 as an example)
Use function PARSE_DATE() :
PARSE_DATE(Delivered_Date, '%Y-%m-%d')
The following doc lists the supported formats.
Cast my columns 'Delivered_Cost' and 'Actual_Cost' to float (it is currently with string type)
Your syntax with CASE() is OK ; you could also use shortcut method FLOAT(). However if your string does not successfully maps to a float (like - alone), a runtime error will occur. You could use SAFE_CAST() to ignore conversion error, but that might also lead to ignoring relevant errors. Hence, you would better use REPLACE().
Here is your query :
SELECT
PARSE_DATE(Delivered_Date, '%Y-%m-%d') AS Delivered_Date,
FLOAT(REPLACE(Delivered_Cost, '-', '0')) AS Delivered_Cost,
FLOAT(REPLACE(Actual_Cost, '-', '0')) AS Actual_Cost
FROM MYTABLE
FLOAT(Delivered_Cost)
Below is for BigQuery Standard SQL
#standardSQL
SELECT
DATE_FROM_UNIX_DATE(date_column_as_number_of_days_since_epoch) date_since_epoch,
IFNULL(SAFE_CAST(Delivered_Cost AS FLOAT64), 0.0) AS Delivered_Cost,
IFNULL(SAFE_CAST(Actual_Cost AS FLOAT64), 0.0) AS Actual_Cost
FROM `project.dataset.table`
You can test, play with it using dummy data as below
#standardSQL
WITH `project.dataset.table` AS (
SELECT
43397 AS date_column_as_number_of_days_since_epoch,
'123' AS Delivered_Cost,
' - ' AS Actual_Cost
)
SELECT
DATE_FROM_UNIX_DATE(date_column_as_number_of_days_since_epoch) date_since_epoch,
IFNULL(SAFE_CAST(Delivered_Cost AS FLOAT64), 0.0) AS Delivered_Cost,
IFNULL(SAFE_CAST(Actual_Cost AS FLOAT64), 0.0) AS Actual_Cost
FROM `project.dataset.table`
with result as
Row date_since_epoch Delivered_Cost Actual_Cost
1 2088-10-25 123.0 0.0
Note: I am assuming that 43379 which you use as an example in your question is actually a number of days since epoch - as this is most reasonable from my point assumption - let us know if this is something else, so I will adjust answer respectively

Error converting data type varchar to decimal, --data type errors

I am altering a view to cast geographic coordinates (numbers with varying decimal precision) into a decimal field.
I have confirmed that the only items in the source tables are numbers and decimals -- but I am getting the error
"Error converting data type varchar to numeric."
Is it possible the decimal in the source table is being read as a character, and if so, what could I do to successfully execute this conversion?
ALTER VIEW [SCHEMA].[VIEW_V]
AS SELECT
cast(field 1 AS decimal (26,19)) as x_coord
FROM [linkedServer].[Sourcedatabase].[schema].[dt_table]
Trying using isnumeric() or try_convert():
ALTER VIEW [SCHEMA].[VIEW_V] AS
SELECT (CASE WHEN isnumeric(field1) = 1
THEN cast(field1 AS decimal(26,19))
END)as x_coord
FROM [linkedServer].[Sourcedatabase].[schema].[dt_table];
or
ALTER VIEW [SCHEMA].[VIEW_V] AS
SELECT try_convert(decimal(26, 19), field1)
FROM [linkedServer].[Sourcedatabase].[schema].[dt_table];
Where the resulting value is NULL, you will know which rows are causing the problems.
In case of float values with characters 'e' '+' it errors out if we try to convert in decimal. ('2.81104e+006'). It still pass ISNUMERIC test.
SELECT ISNUMERIC('2.81104e+006') returns 1.
SELECT convert(decimal(15,2), '2.81104e+006') returns error: Error converting data type varchar to numeric.
SELECT try_convert(decimal(15,2), '2.81104e+006') returns NULL.
SELECT convert(float, '2.81104e+006') it returns correct value 2811040.

Convert from varchar to float SqlServer

I have a column in varchar format and I would like to convert it to float. The values are like:
188234,62
188235
188235,43
188235,88
So, When I try to convert it I get an error:
Error converting data type varchar to float.
I thought that maybe some fields are not numeric and I checked with isnumeric() function, according to the query I concluded that all the values are numeric. Then I tried different queries to convert the field types:
Query1:
SELECT CarData.dbo.FinalData_OilLevel_new_send.KM
CASE WHEN ISNUMERIC(CarData.dbo.FinalData_OilLevel_new_send.KM) = 1
THEN CAST(CarData.dbo.FinalData_OilLevel_new_send.KM AS FLOAT)
ELSE NULL
END
FROM CarData.dbo.FinalData_OilLevel_new_send
Query2:
SELECT CASE ISNUMERIC(KM) WHEN 1 THEN CONVERT(float, KM)) ELSE null END from CarData.dbo.FinalData_OilLevel_new_send
However I always get the same error. Are there some other ways to convert varchar to float?
You have to use a dot as decimal separator:
CAST(REPLACE(CarData.dbo.FinalData_OilLevel_new_send.KM,',','.') AS FLOAT)
At first you need to Replace
The , present
try like this!
select cast(replace(val,',','') as float) from table
SEE DEMO

AVG() not accurate in SQL Server

I am trying to calculate an Avg() for an column with where condition using SQL Server Management Studio 2008. When I try the AVG(column-name) in a SQL query it gives me a rounded value and not a decimal value.
When I copy the dataset into MS Excel I get the accurate value with 4 decimal (example: 10.74) in SQL I am getting just 10. Any help is appreciated.
My query:
SELECT Item, AVG(POOrdrQty)
FROM [tableWR]
where Item ='737' AND POOrdrQty <((select AVG([POOrdrQty]) FROM [tableWR]) * 2)
group by Item
The resulting type of the avg aggregate is the same type as the value you use as parameter. If the field is an int, the result will be rounded to fit the type.
Cast the value that you use as parameter: avg(cast(column-name as float)).
avg(column_name * 1.0) to convert data type to float