?
Solved

mySQL. SQL query. Substitute for Numeric key word.

Posted on 2016-11-02
3
Medium Priority
?
104 Views
Last Modified: 2016-11-02
Let say I need to solve the problem :

With a precision of two decimal places, determine the average number of guns for the battleship classes.


SELECT AVG(numGuns+0.0)
FROM Classes
WHERE type = 'bb'

Open in new window


The query about work fine but show me result with more than 2 decimal points.

I need to write sql query that will be analog query below but will be work on mySQL server ( as far as I understand Numeric key word didn't work in mySQL server )
SELECT CAST ( AVG(numGuns+0.0) AS NUMERIC(10,2) )
FROM Classes
WHERE type = 'bb'

Open in new window


So how can I rewrite the query above in order that it'll work in mySQL server.
Thx in advance !
0
Comment
Question by:SunnyX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Assisted Solution

by:F Igor
F Igor earned 224 total points
ID: 41870557
You can use the FORMAT function


SELECT FORMAT( numeric_expression, decimals) FROM table

Open in new window



http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_format
0
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 1776 total points
ID: 41870612
Try..

--WITH OUT ROUNDING

SELECT TRUNCATE( AVG(numGuns) ,  2 )  AS numGuns
FROM Classes
WHERE type = 'bb'

--WITH ROUNDING

SELECT ROUND( AVG(numGuns) ,  2 )  AS numGuns
FROM Classes
WHERE type = 'bb'
0
 

Author Closing Comment

by:SunnyX
ID: 41870769
thx everybody !
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question