Home > OS >  why cast,convert donot work where round,floor works in the following case
why cast,convert donot work where round,floor works in the following case

Time:11-11

I am a beginner to MySQL, i am trying to solve the following question

''' https://www.hackerrank.com/challenges/average-population/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen '''

where I need to round the average of all cities population, coming from the normal c ,c background I thought casting a decimal to int would round the integer but it's showing the following error

''' ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int) from city' at line 1 ''' for the following code

''' select cast(avg(population) as int) from city; ''' same error with convert as well. could you please why this doesn't work where cast(25.5 as int) works.

CodePudding user response:

When we use CAST or CONVERT function in mysql, the int is not a permitted type. Mysql has SIGNED [INTEGER] or UNSIGNED [INTEGER] for the same.

Reference: MySQL CAST function.

Tip: For rounding in MySQL you can also use Round function.

  • Related