I have a column, called net_amount, it contains values like 244,98. Its a varchar column. When I try to sum it using the sum function, it only sums the 244 and skipts the decimal places. I tried casting it to decimal like this:
select cast(net_amount as decimal) from mytable
This skips the decimal places as well ... any idea what might be wrong?
Thanks!
You could use REPLACE to replace comma to dot:
SELECT REPLACE('244,98', ',', '.') * 1
or you can use CAST like this:
cast(REPLACE(net_amount, ',', '.') as decimal(8,2))
Related
What is the Equivalent of the function Digits in Snowflake?
Any help is appreciated.
Thank you
If this is digits() from DB2
The DIGITS function returns a character string representation of the absolute value of a number.
Then something like:
REPLACE(ABS(<yourcol>), '.', '')
Or:
ARRAY_TO_STRING(SPLIT(ABS(<yourcol>), '.'),'')
If you are also wanting the left-padded zeros there isn't a way to determine precision of a decimal in snowflake directly in your SQL, so you will have to be specific about how large your output string should be.
LPAD(REPLACE(ABS(<yourcol>), '.', ''), 10, '0')
Please try this:
SELECT TRY_TO_NUMBER('abcd');--NULL
SELECT TRY_TO_NUMBER('700000001020577');--700000001020577
SELECT TRY_TO_NUMBER('1234.56');--1234
SELECT TRY_TO_NUMBER('0.56');--0
SELECT TRY_TO_NUMBER('a12#$');--NULL
SELECT IFF(TRY_TO_NUMBER('1234') IS NULL,FALSE,TRUE) --TRUE
SELECT IFF(TRY_TO_NUMBER('abcd') IS NULL,FALSE,TRUE)--FALSE
In my SQL database I have the prices in VARCHAR but I've problem when I try to order by price..
I have try to use this query but doesn't work..!
CAST(price AS DECIMAL) DESC
If I launch this query the result order is:
1.123,45
122,00
2.543,21
656,00
etc
Use a replace to create a decimal point, then cast it with the precision and scale...
cast(replace(Price, ',', '.') as decimal(9,2))
you can use the direct "convert" in order by
ORDER BY convert( [name column], decimal)
I have find the problem.. The numbers > of 999,99 ha the . under the miles..
For example:
1.000,00
I have replaced all . with this query:
UPDATE computers SET prezzo = REPLACE(prezzo, '.', '')
Create a one example as per bolow:
Create a table
create table table1(
id integer,
price varchar(20)
)
Insert record price into varchar:
insert into table1 values(1, '10.1');
insert into table1 values(1, '100.1');
insert into table1 values(1, '20.1');
insert into table1 values(1, '200.1');
Sorting on varchar price as per numeric field:
SELECT * FROM `table1` order by cast(replace(price, ',', '.') as decimal(9,2))
I think that if your commas are thousands separators then you need to replace them with empty strings. If your commas are decimal separators then you need to replace them with decimal points.
Try this if your commas are thousands separators:
SELECT
price
FROM
prices
ORDER BY
CAST(REPLACE(price,',','') AS DECIMAL)
This should give you:
price
543,21
123,45
1,001
6,00
2,00
If your commas are decimal separators then this:
SELECT
price
FROM
prices
ORDER BY
CAST(REPLACE(price,',','.') AS DECIMAL)
Will give you:
price
543,21
123,45
6,00
2,00
1,001
Check This.
select * ,REPLACE( price, ',', '' ) from money
order by CAST(REPLACE(price,',','') AS DECIMAL)
Check Demo Here : Link
I'd like to extract the number between NUMBER and ;. So far I can extract the data up to the number, but I don't want anything after the number. e.g.,
SELECT
SUBSTRING(field, LOCATE('NUMBER=', rrule) + 7)
FROM table
Data field:
DATA:PASS=X12;NUMBER=331;FIELD=1
DATA:PASS=X12;NUMBER=2;FOO=BAR;FIELD=1
Desired Output:
331
2
You can use a combination of SUBSTRING_INDEX functions:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(field, 'NUMBER=', -1),
';',
1)
FROM
tablename
Please see an example fiddle here.
The inner SUBSTRING_INDEX will return everything after the NUMBER= string, while the second will return everything before the ; returned by the inner function.
I am using a mySQL statement that returns me average of values as comma separated integer.
Eg : 2,109. But I want my output to be plain integer like 2109. Please help me on this.
You can use something like this:
SELECT REPLACE(fieldname, ',', '')
FROM ...
Or if type of fieldname is integer use this query
SELECT REPLACE(CONCAT(fieldname), ',', '')
FROM ...
i have a mysql table with this sort of data
TACOMA, Washington, 98477
Now i have thousands of such rows. I want the data to be manipulated in such a manner that it appears like:
TACOMA, Washington
Is it possible though mysql or do i have to manually do it.
You can use :
SELECT SUBSTRING_INDEX('TACOMA, Washington, 98477', ',', 2)
You can read more here.
And the update statement :
UPDATE my_table
SET my_col = SUBSTRING_INDEX(my_col, ',', 2)
Where you need to replace my_table with your table name and my_col with the column you need to be updated.
Possibly this way. Count the number of commas (by checking the length against the length with all the commas removed) and then use SUBSTRING_INDEX to get the string up to the number of commas:-
SELECT SUBSTRING_INDEX(col, ',', LENGTH(col) - LENGTH(REPLACE(col, ',', '')))
FROM SomeTable
substring_index(col, ',',-1)
will give the string from last index of comma to end of string
replace(col,concat(',',substring_index(col, ',',-1)),'')