Currently I have this field in my database that is a (16, 10) double. I need to change this to a ( 16, 4 ) double. the only problem is that I have a lot of records and I need to check if some any of those records are using more than 4 decimal places. Something like 1500.3333333. The problem is that by changing just changing the field to (16,4) I get zeros to the right, like 14,5500.
Is there any way, using Mysql that I can check if the double is using more than 4 decimal places that are not 0?
Use CHAR_LENGTH() and LIKE functions, like this:
SELECT * FROM table
WHERE CHAR_LENGTH(column) > 4
AND column NOT LIKE "%0"
Related
I have a row who contains this value :
account :
adm.ahrgrst001
adm.ns2dhdujhd
adm.ff2hdjhh
adm.haidhidh103
adm.hshiksh122
adm.cn3ehuioe
i want to extract two different values:
when it ends like adm.hshiksh122 i want to extract hshiksh
and with start with adm.cn3ehuioe i want ehuioe
both without the adm. at the beginning
I have thinked this
IIF(isnumeric(RIGHT (account,3)),LEFT(account,LEN(account)-4),RIGHT (account,LEN(account)-7))
the value that are like adm.cn3ehuioe i got wrong like adm.cn3ehui
and adm.ahrgrst001 is correct ahrgrst
Thanks to everyone who will read
The correct way to get the 2 types of values is with:
account LIKE 'adm.[!0-9][!0-9]#[!0-9]*'
for the values that have 2 letters, 1 digit and letters after the dot, and:
account LIKE 'adm.*###'
for the values that end in 3 digits.
So use this:
SELECT IIF(
account LIKE 'adm.*###' ,
MID(account, 5, LEN(account) - 7),
MID(account, 8)
) AS result
FROM tablename
WHERE account LIKE 'adm.*###' OR account LIKE 'adm.[!0-9][!0-9]#[!0-9]*'
If there are no other values than these 2 types then you may remove the WHERE clause.
Results for your sample data:
result
ahrgrst
dhdujhd
hdjhh
haidhidh
hshiksh
ehuioe
I am in SQL Workbench and want my output to include 4 decimal places. I have tried different combinations of casting 'sessions', 'transactions', and 'cvr' as DECIMAL as well as using ROUND, and I can't seem to get the output I'm looking for. 'sessions' and 'transactions' are both in NUMERIC(19,2) format.
Ideally, I want to stay away from casting to float to avoid losing precision.
select cast((transactions/sessions) as decimal(10,5)) as cvr
from(select name
,sum(cast(sessions as decimal(10,5))) as sessions
,sum(cast(transactions as decimal(10,5))) as transactions
How to get output with 4 decimal places
SELECT FORMAT(123.456789, 4) as col
123.4567
SELECT FORMAT(123.456, 4) as col
123.4560
I need round up the numbers of a complete table of mysql with a funtion leaving the two decimals to zero?
e.g.:
from 75,55 to 86,00
from 75,45 to 75,00
All the prices of a complete table changed a few decimal and I do not know how it happened.
Can anybody help me?
You can simply use Round(X, D) function. From Docs:
Rounds the argument X to D decimal places. The rounding algorithm
depends on the data type of X. D defaults to 0 if not specified. D can
be negative to cause D digits left of the decimal point of the value X
to become zero.
Try:
SELECT ROUND(75.45, 2);
-- Fetches 75.00
SELECT ROUND(75.55, 2);
-- Fetches 76.00
Now, it seems that you want to Update all the values of a particular column, by rounding them to two decimal places. You can do the following:
UPDATE your_table_name
SET your_column_name = ROUND(your_column_name, 2);
-- It will round all the values to 2 decimal places.
"UPDATE your_table_name SET your_column_name = ROUND(your_column_name, 2);" not work for me. I dont know.
¡SOLUTION! = TABLE > ESTRUCTURE > NAME COLUMN > length/values > remove decimal.
All number round up in my row (column). Them I put decimal again. All my fields it's ok.
I an using MySQL database and I have a table called fertilizer_storage which is using both plus and minus values. It has 4 columns uria,TSP,MOP and TDM
I am using double as data type and getting sum of each column using follwing syntax,
SELECT SUM(uria),SUM(TSP),SUM(MOP),SUM(TDM) FROM `fertilizer_storage` WHERE `branch_ID`=1
The problem is for some columns I get unwanted floating points of 15 while all columns are containing numbers up to 4 floating points.
7.666900000000002
7.666900000000002
9.6109
9.9924
when I changed numbers in first two columns as other two it gives the correct answer. what should I do to correct this.
You can use ROUND(X, D):
Rounds the argument X to D decimal places. The rounding algorithm
depends on the data type of X. D defaults to 0 if not specified. D can
be negative to cause D digits left of the decimal point of the value X
to become zero.
SELECT Round(Sum(uria), 4),
Round(Sum(tsp), 4),
Round(Sum(mop), 4),
Round(Sum(tdm), 4)
FROM `fertilizer_storage`
WHERE `branch_id` = 1
See it in action
I have a csv file that contains phone numbers, some of them have 9 digits and some of them have 10. Is there a command that would allow the transformation of the column such that numbers that have only 9 digits will have a 0 appended in front of the numbers.
For example,
if the column has values "443332332" and "0441223332", I would like to have the value of the one with 9 digits changed to "0443332332"?
Sorry, I should have elaborated.
I was wondering if there was a command to do it in SQLlite easily? I prefer not to use excel to transform the column as if I can get it to working with sqllite it would be so much easier and faster.
A more generic solution would be:
select substr('0000000000'||'1234567', -10, 10) from table_name;
The above query would always return 10 digits and add leading zeroes to the missed out number of digits.
For example, the above query would return : 0001234567
For Update, use
UPDATE TABLE_NAME SET PHONE_NO = substr('0000000000'|| PHONE_NO, -10, 10);
If you're sure that just prepending a zero on strings with length 9 will work for your application, something simple will work:
SELECT CASE WHEN LENGTH(phone_number) = 9 THEN '0'||phone_number
ELSE phone_number
END AS phone_number
FROM your_table
;
You could also update the table, depending on your needs:
UPDATE your_table
SET phone_number = '0'||phone_number
WHERE LENGTH(phone_number) = 9
;
Open the .csv using Excel,
Add a filter to the column,
Sort from A-Z to get all the columns with 9 digits,
Then follow the steps here
http://office.microsoft.com/en-au/excel-help/keep-leading-zeros-in-number-codes-HA010342581.aspx