I am trying to extract the length of an integer column but I get this error as below. Can someone share their thoughts on this? Thanks.
Error: Argument type mismatch in function LENGTH: first argument is type int64
LENGTH is a function which operates on strings. If you want the string length of an integer, you could run, e.g.:
SELECT LENGTH(STRING(17));
You can also see the BigQuery query reference for string functions for more information.
If you have a GBQ Integer type (INT64) then most likely you have to do this nowadays:
SELECT LENGTH(CAST(17 AS STRING));
Related
the thing is to replace whatever observation that hold the number 999.9 as NULL
but i dont know the utility of CAST here as i know it helps change the data type but WDSP in the description is STRING
IF(
wdsp="999.9",
NULL,
CAST(wdsp AS Float64)) AS wind_speed,
nothing yet the cast should it come before IF or within IF
The CAST function in being used to convert the wdsp column to a number (specifically FLOAT64) instead of returning it as a STRING (which you mention is the declared type). This is likely so that whatever is issuing the query can get the aliased column wind_speed as a numerical type that can be processed as a number and not a string.
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 have an entity. One field named "number" consists of String. It is a number + some text information. For example:
131-MOD
11853-ARO
983-AKK
etc.
My task is: get the maximum of the first number. So, I have to extract Integer value from String "number" and find the maximum from it. For the examples higher, it would be the numbers 131, 11853 and 983. So, the maximum is 11853. I have to get this Integer value as a result.
Here i have my try using Hibernate. But it working with only Integer values. How to extract number, i have no idea.
public Integer getMaxNumber()
{
return (Integer) getSessionFactory().getCurrentSession().createQuery("select max(id) from EmployeeTripCard s").uniqueResult();
}
How can i do that?
You may use the following JPQL query:
SELECT
MAX(CAST(SUBSTRING(id, 1, LOCATE(id, '-') - 1) AS INTEGER))
FROM EmployeeTripCard s;
We can use LOCATE to find the index of the first -, then call SUBSTRING to find the initial number. Note carefully that we also need to cast this resulting string to an integer, in order for MAX to behave the way we want (numbers as text don't always sort the same way as actual pure numbers).
I am trying to find the minimum length by getting the length of each values in a column (double) in a table and running a min function on top of it to get the minimum length.
This works well when the column is a string type but the 'length' function does not work for double datatype in impala, what is the other way to address this?
min(length(columnname))
All double columns are 8 bytes, as explained in the documentation. LENGTH() is a string function and it doesn't really make sense on a numeric value (although you can convert to a string and then measure the length).
I am doing the sum() of an integer column and I want to typecast the result to be a bigint - to avoid an error. However when I try to use sum(myvalue)::bigint it still gives me an out of range error.
Is there anything that I can do to the query to get this to work? Or do I have to change the column type to a bigint?
The current manual is more explicit than it used to be in 2013:
sum ( integer ) → bigint
If your column myvalue indeed has the type integer like you say, then the result is bigint anyway, and the added cast in sum(myvalue)::bigint is just noise.
Either way, to get an "out of range" error, the result would have to be bigger than what bigint can hold:
-9223372036854775808 to +9223372036854775807
One would have to aggregate a huge number of big integer values (>= 2^32 * 2^31). If so, cast the base column to bigint, thereby forcing the result to be numeric:
SELECT sum(myvalue::int8) ...
The more likely explanation is that your column has, in fact, a different data type, or the error originates from something else. Not enough detail in the question.
I solved my problem using following statement
SUM(CAST(gross_amount AS Integer))
This is give the result of the column as SUm bigint,
Note:My column gross_amount was double type.
You need to cast it before doing the operation:
SUM(myvalue::bigint)