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)
Related
I have method like this:
def self.weighted_average(column)
sql = "SUM(#{column} * market_cap) / SUM(market_cap) as weighted_average"
Company.select(sql).to_a.first.weighted_average
end
When the column is a decimal, it returns a value without problem.
But when the column is integer, the method ends up with a PG::NumericValueOutOfRange error.
Should I change column type integer to decimal, or is there a way to get the result of sum without changing column type?
You can always make float from your integer.
def self.weighted_average(column)
column = column.to_f
sql = "SUM(#{column} * market_cap) / SUM(market_cap) as weighted_average"
Company.select(sql).to_a.first.weighted_average
end
You can cast your value to alway be a decimal value, thus no need to change the column type:
sql = "SUM(#{column} * CAST(market_cap as decimal(53,8))) / SUM(CAST(market_cap as decimal(53,8))) as weighted_average"
P.S. I would go with changing the column type - it is consistent then.
I would suggest you to change the datatype to decimal. Because, when SUM gets PG::NumericValueOutOfRange, it means that your datatype is not sufficient. It will lead to gracefully handle this scenario, instead of a workaround.
Postgres documentation says this about SUM() return type:
bigint for smallint or int arguments, numeric for bigint arguments,
otherwise the same as the argument data type
This means that you will somehow need to change datatype that you pass to SUM. It can be one of the following:
Alter table to change column datatype.
Cast column to other datatype in your method.
Create a view that casts all integer columns to numeric and use that in your method.
You are trying to place a decimal value into a integer parameter. Unless you use the ABS() value that will not be possible, unless you are 100% sure that the % value will always be 0.
Use type Float or function ABS() if you HAVE to have an INT
Yo could try casting column to decimal
sql = "SUM(CAST(#{column}) AS DECIMAL * market_cap) / SUM(market_cap) as weighted_average"
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.
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));
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)
I have a nvarchar(200) column in a table that contains a mix of integers (as strings) and non-integer strings and symbols also. E.g. Some sample data :-
Excuse me for my typing in my initial post I mentioned varchar(200) but in fact it is 'nvarchar(200)'
02
0
.../
125
00125
/2009
1000
0002589
000.00125
Marathi numbers like & letters
how can I order this Column?
You can use CAST to convert a varchar to an INT given that varchar is holding a proper number.
SELECT CAST(varCharCol as Int)
E.g.
col1 as Varchar(10)
col1 = '100' converting to INT will be successufl
but if col1 = '100 xyz' will be unsucessful in the process.
Looking at your string you may have to use number of LTRIM, REPLACE to get hold of the digits or using a regex to get comma separated numbers. That too it's not very clear how your original string looks like.
References.
Many DBMS have CAST() functions where you can convert one datatype to another.
For MySQL have a look at this site
You can Use CAST and Convert to convert string type value to int type. but be sure the value should numeric.
select convert(int,'123')
select CAST('123' as int)
You can use this query
SELECT CASE
WHEN ISNUMERIC(colName)=1 THEN ROUND(colName, 0)
ELSE 0 END AS [colName]
FROM tblName