For what DBMS, besides SQL Server, you would like to have exercises on the site?
SQL Exercises news letters, latest issue
#305 (2010-07-31)
Week news
§
As the important news can appear and between releases of news letters, I shall use this page for the publication of the information on all changes on the site.
The forum is not so suitable for this purpose since the message there "escapes" and can appear on previous page before it will lose a urgency.
So look in here if you wish to be well informed about last events on the site. The link to this page is in section of news at the main page below a subscription banner.
§
Searching and bookmarks service have appeared in the site forum.
§
Two new exercises - #166 (anddros,
complexity 1 point), #167 (qwrqwr, 2 points) are added to the third stage.
§
Has corrected a bug with checking the exercises:
82 (rating stage) - hint from author (_Virt_).
§
Changes among leaders (solved exercises of third stage):
polishchuk (A10055348) [BK] - г. Москва, Россия Easy-Human (A10070369) [BK] - г. Оренбург, Россия
§
Statistics:
Number of subscribers - 3825
Number of rating's participants - 9607
Number of second-stage's participants - 2162
Number of third-stage's participants - 70
On the learning stage - 16523
Certified specialists in total - 451
Once in a social network was asked how to remove trailing zeros in decimal numbers. This was associated with the preparation of a report, which sums concatenate with the text. In the conditions of the problem was stated limit to two decimal places.
Here's an example of data and the desired result:
given to obtain
0.00 0
10.00 10
2.50 2.5
100.00 100
11.33 11.33
Solutions have been proposed, based on analysis of the line. I also chose the wrong way and proposed the following solution:
SELECT num,
CASE WHEN CAST(num AS INT) = num
THEN CAST(CAST(num AS INT) AS VARCHAR)
WHEN CAST(num*10 AS INT) = num*10
THEN LEFT(CAST(num AS VARCHAR), LEN(CAST(num AS VARCHAR)) - 1)
WHEN CAST(num*100 AS INT)=num*100
THEN CAST(num AS VARCHAR)
END fnum
FROM(
SELECT 0.00 as num
UNION ALL SELECT 10.00
UNION ALL SELECT 2.50
UNION ALL SELECT 100
UNION ALL SELECT 11.33
) X
I do not know how much it would still be continued, if one member did not notice that all problems are solved by conversion to the data type FLOAT. Really:
SELECT num, CAST(num AS FLOAT) fnum
FROM(
SELECT 0.00 as num
UNION ALL SELECT 10.00
UNION ALL SELECT 2.50
UNION ALL SELECT 100
UNION ALL SELECT 11.33
) X
However, you need to remember the approximate nature of this type, namely the magnitude of the mantissa in scientific representation of the number.
In accordance with the standard in this type of data specified argument - FLOAT (n), which can take values from 1 to 53. The SQL Server, an argument value in the range 1 - 24, interprets it as 24, which corresponds to the accuracy of 7 digits, and in the range 25 - 53 as 53, which corresponds to the accuracy of 15 digits. The default is 53.
The following example illustrates the above:
SELECT num,
CAST(num AS FLOAT(24)) num_24,
CAST(num AS FLOAT(53)) num_53
FROM(
SELECT 1234567.80 AS num
UNION ALL SELECT 12345678.90
UNION ALL SELECT 123456789012345.60
UNION ALL SELECT 1234567890123456.70
) x
num num_24 num_53
1234567.80 1234568 1234567,8
12345678.90 1,234568E+07 12345678,9
123456789012345.60 1,234568E+14 123456789012346
1234567890123456.70 1,234568E+15 1,23456789012346E+15
MySQL (version 5.0)
Does not support the conversion to type FLOAT.
PostgreSQL (version 8.3.6)
Almost similar behavior, except that for the parameter in the range 1 - 24 precision is 6 digits. Accordingly, recent results will look like this: