MySQL add 0 when < *.10 [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't seem to find/create a working query which will update the x column with an extra zero when value after the dot is below .10 - I've got column x with values;
7.3
5.3
0.14
I am trying to update the column so when the value after the dot is below .10 it adds a zero, above example will eventually need to look like;
7.03
5.03
0.14
Is there a way I can do this using an mysql UPDATE-query?
Many thanks.

First of all, 0.03 is not equal to 0.30, so i believe what you want is 7.30 and not 7.03. To do this, you need to alter your table column to display two decimal digits:
my_magic_column float(3,2)
where 3 is the total number of digits and 2 is the number of decimals to show.

I don't really get idea behind such operation but it's pretty simple.
SELECT CASE
WHEN INSTR(id, '.') = 0 THEN id
ELSE CASE
WHEN CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED) < 10 THEN
CONCAT(SUBSTRING_INDEX(id, '.', 1), '.0', CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED))
ELSE id
END
END as result
FROM table_name
UPDATE table_name SET id = CASE
WHEN INSTR(id, '.') = 0 THEN id
ELSE CASE
WHEN CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED) < 10 THEN
CONCAT(SUBSTRING_INDEX(id, '.', 1), '.0', CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED))
ELSE id
END
END
But before using this, think once again about your schema and what are you trying to do. I don't think you'll need this then.

Looks like what you want to do is:
update TABLE set YOUR_NUMBER = (FLOOR(NUMBER) + (NUMBER-FLOOR(NUMBER))/10)
where FLOOR(NUMBER*10) = NUMBER*10
FLOOR(NUMBER) - take the integer part and add to it:
(NUMBER-FLOOR(NUMBER))/10 - the fraction part of the number - divided by 10
The where condition checks that there's only one digit after the dot

Related

Query for students passed or not passed in specific Subjects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a Table with Column Headings:
ID Student_Name
Roll_Number
Subject1MarksObtained
Subject1TotalMarks
Subject2MarksObtained
Subject2TotalMarks
Subject3MarksObtained
Subject3TotalMarks
Subject4MarksObtained
Subject4TotalMarks
I want to write a query to output the results for individual student who have pass at least three of the subjects.
Without seeing the data, lets make some assumptions:
A pass is awarded for a subject if the marks obtained for that subject are equal to or more than 50% of the total marks available for that subject.
The name of the table is called Enrollment
To return a list of students who have passed at least 3 subjects we can use a query similar to the following:
This solution uses CASE to evaluate a 1 for a pass and a 0 for fail for each subject, then we sum those results and only return rows that have a score of 3 or more.
SELECT ID, Student_Name, Roll_Number
FROM Enrollment
WHERE
( CASE WHEN (Subject1MarksObtained / Subject1TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject2MarksObtained / Subject2TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject3MarksObtained / Subject3TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject4MarksObtained / Subject4TotalMarks) >= 0.5 THEN 1 ELSE 0 END
) >= 3
There are different way to approach this, but this query is simple to read and gets the job done.
If you are querying an access table, then CASE WHEN is not supported but you can use IIF or SWITCH to achieve the same results:
SELECT ID, Student_Name, Roll_Number
FROM Enrollment
WHERE
( IIF( (Subject1MarksObtained / Subject1TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject2MarksObtained / Subject2TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject3MarksObtained / Subject3TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject4MarksObtained / Subject4TotalMarks) >= 0.5, 1, 0)
) >= 3
Let's instead start by fixing your broken schema. A normalised design might look somewhat as follows:
Student
ID
Student_Name
Roll_Number
Results
StudentID
Subject
Mark

SQL How to sort by "Ranges"

I am currently using a database with different entries like dates, names, but also one column with time "ranges". This basically means that there can be a definite number like "10" in this cell, but also a value like "10-15" or "5-10".
So what I want to do here is to sort them by an "average" value ((Lowest+Highest)/2). So in case of the 3 mentioned values it should be
5-10
10
10-15
I am wondering if it is possible to embed this into the SQL statement in some way.
And if it is not possible, I'd like to know the easiest way to implement it otherwise.
Right now I am putting the $SQL_statement together via several conditions, then putting everything into $resultset which is then used with "while". Here are some snippets:
$resultset=mysql_query($SQL_statement);
while ($currententry=mysql_fetch_array($resultset))
{
echo $currententry['Platform'];
echo $currententry['PlaytimeInH']."h";
}
You can do this with substring_index() and arithmetic:
order by (substring_index(col, '-', 1) + 0 +
substring_index(col, '-', -1) + 0
) / 2
The division by 2 is unnecessary, but you do specify the average in your question.
Note that the above will work even if col has no hyphen in it.
You could use a select with a case when clause in order by
select col1, col2, cole
from your_table
order by case
when your_column = '5-10' then 1
when your_column = '10-15' then 2
when your_column = '15-20' then 3
else 4
end

Split numbers in database separated by a dash (-)

I am trying to split a column with over 4 millions rows into 4 new columns, The problem is i haven't a clue where to look or which term I should Google search. (Yes i have searched Google and Stack for a similar question but only found one on stack that might be a good fix but not sure if it would help, Added link further down to the question in question.)
Here is my DB.
ID Security
1 1-5-4-6
2 2-4-06-5
3 1-4-1-2
4 1-4-1-3
5 1-45-5-32
What i am trying to do is the following, The "security" Column need to be split in 4 separate columns (group,lvl,key,code)
ID group lvl key code
1 1 5 4 6
2 2 4 6 5
3 1 4 1 2
4 1 4 1 3
5 1 45 5 32
The problem i am having is finding a suitable solution to split these numbers, one issue is the current security column can have up to 3 characters per - separation.
i could have a row that has 01-45-822-01 or as simply as 1-2-3-4.
One question i did find on stackoverflow
MySQL : how to split text and number with "-"
It does seem to point me in the right direction but still unsure if this would be suitable for such a large data set. over 5.9gb indexed or if there is a way to do this quicker.
You can use SUBSTRING_INDEX to do this all in one query:
UPDATE `table`
SET
`group` = SUBSTRING_INDEX(`Security`, '-', 1),
`lvl` = SUBSTRING_INDEX(SUBSTRING_INDEX(`Security`, '-', 2), '-', -1),
`key` = SUBSTRING_INDEX(SUBSTRING_INDEX(`Security`, '-', 3), '-', -1),
`code` = SUBSTRING_INDEX(`Security`, '-', -1);
And around each of those you'll probably want to wrap a CAST(expr AS UNSIGNED) assuming you're no longer storing numbers as text.

Using multiple OR in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to write a query which should check if there is at least one column which has a value greater than 0 (>0) in the table. Currently, I have written a 'WHERE' clause with multiple OR conditions. I suspect that this may not be the best way of doing this (may be use SUM?). Seeking opinion from SQL experts.
WHERE raw_0 >0 OR raw_1 >0 OR raw_2 >0 OR raw_3 >0 OR raw_4>0
You can use
WHERE (raw_0 >0 OR raw_1 >0 OR raw_2 >0 OR raw_3 >0 OR raw_4>0);
As per your question. As dnoeth commented, is the easiest to write and understand (and probably the best for the optimizer as there's no calculation involved)
Alternatively you can use:
WHERE ((RAW_0 + RAW_1 + RAW_2 + RAW_3) > 0);
The above condition would be satisfied IF and only IF, atleast one of the values is >0.
You can also use concat operator to concat the strings and check if the concatenated string has value other than zero:
WHERE TO_NUMBER ( RAW_0 || RAW_1 || RAW_2 || RAW_3 ) > 0
Test data for 3rd query:
select 'YES value' data from dual where TO_NUMBER ( 1 || 2) > 0 ;
select 'NO value' data from dual where TO_NUMBER ( 0 || 0) > 0 ;
If the values can only be non-negative, as in one of the OP comment then you can check the multiplication
WHERE raw_0 * raw_1 * raw_2 * raw_3 * raw_4 > 0
but that will check if every value as positive. To check if only one is positive we can negate this logic using the SIGN function
WHERE (1 - SIGN(raw_0))
* (1 - SIGN(raw_1))
* (1 - SIGN(raw_2))
* (1 - SIGN(raw_3))
* (1 - SIGN(raw_4)) = 0
(1 - SIGN(value)) return 1 when the value is zero and 0 when the value is positive, so if at least one value is positive the product will be 0.

Transforming a column to have 10 Digits

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