Solved

mySQL. SQL query. Substitute for Numeric key word.

Posted on 2016-11-02
3
59 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
3 Comments
 
LVL 13

Assisted Solution

by:F Igor
F Igor earned 56 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 28

Accepted Solution

by:
Pawan Kumar earned 444 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql ide 10 39
Excel - SQL export question 3 41
SQL Server 2012 r2 Make faster Temp Table 17 101
Please help with the below query - SQL Server 11 16
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now