Reputation: 361
How can I remove zeros (not just leading zeros, but zeros in every position) from a column containing Integers(numbers)?
For example : 0349010330 should become 349133
Upvotes: 33
Views: 122714
Reputation: 37
In MS SQL, as @hrshd has mentioned, his answer was:
SELECT CEILING(AVG(SALARY))- CEILING(AVG(CAST(REPLACE(SALARY,'0','')AS
INT))) +1 FROM EMPLOYEES;
The above problem of adding +1 would not occur when both values are of the same datatype. So, a possible solution could be:
select ceiling( avg(cast(Salary as real)) -
avg(cast(replace(Salary,0,'') as real)) )
from EMPLOYEES;
While answering the original question, in MS SQL it is:
cast( replace(a_number,0,'') as real )
Upvotes: 0
Reputation: 812
Here is the solution (MySQL) for Hackerrank challenge The Blunder
SELECT CEIL((AVG(salary)) - (AVG(REPLACE(salary, '0', '')))) AS avg_salary FROM employees;
Upvotes: 61
Reputation: 9
HackerRank Solution https://www.hackerrank.com/challenges/the-blunder/problem
in SQL Server
SELECT CAST(CEILING (AVG(CAST(SALARY AS float)) - AVG(CAST(REPLACE(salary, '0', '')AS float) ) ) as int) FROM EMPLOYEES;
Here, the actual average salary comes out to be 4046.75 and miscalculated avg salary comes out as 1794.5 Hence the difference 2252.25, which when rounded up is 2253.0, but the hackerrank solution expects it as int, so we need to again cast it back to int.
Upvotes: -1
Reputation: 57
This is the answer for removing 0 in any number.
replace('0349010330', '0', '')
Upvotes: 0
Reputation: 1
select
avg(salary) - avg(convert(integer, replace(salary,'0', ''))) + 1
from employees
This is the answer
Upvotes: 0
Reputation: 85
Here is the answer using MySQL for Hackerrank problem:
SELECT round(avg(salary),0)-round(avg(replace(salary,'0','')),0) from EMPLOYEES;
you can use:
replace(Column_Name,'0','')
the above given query to remove 0 from the given integer type input
Upvotes: 0
Reputation: 494
No need to convert it into string to use replace
MySQL solution
SELECT CEIL(AVG(SALARY)-AVG(REPLACE(SALARY,"0",""))) FROM EMPLOYEES ;
Upvotes: 10
Reputation: 1001
HakerRank solution for SQL Server
SELECT CEILING(AVG(SALARY))- CEILING(AVG(CAST(REPLACE(SALARY,'0','')AS INT))) +1 FROM EMPLOYEES;
I do not know why the '+1' in the end is needed to get the correct answer.
Upvotes: 5
Reputation: 470
Here is answer
replace(salary,0)
A question similar to this found in hackerrank..blunder problem
select ceil(avg(salary)- avg(replace(salary,0))) from employees;
Upvotes: 1
Reputation: 844
You can use REPLACE in Oracle but remember to convert the result to number because REPLACE returns CHAR result.
select to_number(replace(1001,0,'')) from dual
Upvotes: 0
Reputation: 43
I think you do not need to_number( replace( to_char(x), '0', '' ) )
Instead you can simply type
replace(x, '0')
and the casting is applyed automaticaly.
Also, you do not have to specify 0 to be empty replace(x, '0', '')
cause replace()
by default removes the first value if you do not specify its alternative.
Upvotes: 0
Reputation: 173
https://www.hackerrank.com/challenges/the-blunder/problem
For Mysql, Please run below query
select ceil(Avg(Salary) - Avg(CAST(Replace(CAST(Salary as char(1000)),'0','') as unsigned))) from EMPLOYEES
Upvotes: 1
Reputation: 177
MYSQL:
REPLACE('0349010330', '0', '')
This will replace all the Os in the string with an empty string.
Upvotes: 2
Reputation: 21
HackerRank Mysql Solution https://www.hackerrank.com/challenges/the-blunder/problem
SELECT CEIL((AVG(SALARY))-(AVG(REPLACE(SALARY,'0','')))) FROM EMPLOYEES
Upvotes: 1
Reputation: 441
Oracle/PLSQL
SELECT TO_NUMBER(REPLACE(TO_CHAR(0349010330),'0','')) FROM DUAL;
MySQL
SELECT CONVERT(REPLACE(STR(0349010330),'0',''),INTEGER) FROM DUAL;
Upvotes: 7
Reputation: 428
Solution Here ->
`SELECT ROUND(AVG(SALARY)-AVG(TO_NUMBER(REPLACE(TO_CHAR(SALARY),'0')))+.5,0) FROM EMPLOYEES;`
Upvotes: 5
Reputation: 48197
REPLACE('0349010330', '0', '');
this also work
REPLACE('0349010330', '0');
Upvotes: 29