Solved

mySQL. SQL query. Substitute for Numeric key word.

Posted on 2016-11-02
3
87 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 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-sql Date Format 9 62
Syntax issue with my Where Clause SQL 2012 20 42
T-SQL: How to extract records into a new table 7 42
How to add an Index to a date/time field? 15 38
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

742 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