简体   繁体   中英

SQL Converting to Decimal then Round to Nearest Half Number

I have 1 Table:

Table 1:

CREATE TABLE #TempTable (Time decimal(10,6), ID int)
INSERT INTO #TempTable (Time, ID) VALUES (0.5,1), (1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,0.10),(10,11)

Which gives me:

    select * from #TempTable

+---------+----+
| Time    | ID |
+---------+----+
|0.500000 | 1  |
|1.000000 | 2  |
|2.000000 | 3  |
|3.000000 | 4  |
|4.000000 | 5  |
|5.000000 | 6  |
|6.000000 | 7  |
|7.000000 | 8  |
|8.000000 | 9  |
|9.000000 |10  |
|10.000000|11  |
+---------+----+

I would like to get @Number, which is based on time @Time (Values in Col1). To get this I am doing the below:

    SET @Number = (CASE @Time
                        WHEN 00.50 THEN 0.016000
                        WHEN 01.00 THEN 0.013300
                        WHEN 01.50 THEN 0.012650
                        WHEN 02.00 THEN 0.012000
                        WHEN 02.50 THEN 0.011200
                        WHEN 03.00 THEN 0.010400
                        WHEN 03.50 THEN 0.010150
                        WHEN 04.00 THEN 0.009900
                        WHEN 04.50 THEN 0.009700
                        WHEN 05.00 THEN 0.009500
                        WHEN 05.50 THEN 0.009450
                        WHEN 06.00 THEN 0.009400
                        WHEN 06.50 THEN 0.009300
                        WHEN 07.00 THEN 0.009200
                        WHEN 07.50 THEN 0.009200
                        WHEN 08.00 THEN 0.009200
                        WHEN 08.50 THEN 0.009100
                        WHEN 09.00 THEN 0.009000
                        WHEN 09.50 THEN 0.009000
                        WHEN 10.00 THEN 0.009000
                        ELSE 9.999999
                    END); 

The issue is @Time is a user input data (which is enforced to input in the following format hh.hh [00.50 = 30 minutes], which defaults to varchar when they input the values). To ensure @Number never = 9.999999 unless it is > 10.00, I am trying to do the below logic:

 IF @Time > 00.00 AND @Time <= 10.00 THEN round to .5 and convert to decimal(10,2)
 ELSE convert to decimal(10,6)


    SELECT  convert(decimal(10,6),@TimeA), 
CASE
  WHEN convert(decimal(10,6), @TimeA) >= 0.0 AND convert(decimal(10,6), @TimeA) <= 10.0 THEN convert(decimal(10,2),round(@TimeA * 2,0)/2)
  ELSE convert(decimal(10,6),round(@TimeA * 2,0)/2)
END
    FROM azteca.Table 1 WHERE ID = @ID

SET @Time = @TimeA

The above gives me: nvarchar 04.50 failed to convert to data type int. Which means there is something wrong with the case statement. I expect the below results -

User Input: 00.50 @Number = 0.016000
User Input: 01.50 @Number = 0.012650
User Input: 05.78 @Number = 0.009400
User Input: 07.90 @Number = 0.009200
User Input: 09.12 @Number = 0.009000

Your approach seems very complicated. Why not just write the CASE using inequalities:

DECLARE @Time2 DECIMAL(10, 6);
SET @Time2 = CAST(@Time AS DECIMAL(10, 6));

SET @Number = (CASE WHEN @Time2 <= 00.50 THEN 0.016000
                    WHEN @Time2 <= 01.00 THEN 0.013300
                    WHEN @Time2 <= 01.50 THEN 0.012650
                    . . .
               END);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM