简体   繁体   English

SQL转换为十进制然后四舍五入为最接近的半数

[英]SQL Converting to Decimal then Round to Nearest Half Number

I have 1 Table: 我有1张桌子:

Table 1: 表格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). 我想获取@Number,它基于时间@Time(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). 问题是@Time是用户输入的数据(强制以以下格式hh.hh [00.50 = 30 minutes]输入,当他们输入值时默认为varchar)。 To ensure @Number never = 9.999999 unless it is > 10.00, I am trying to do the below logic: 为了确保@Number从不= 9.999999,除非它> 10.00,否则我尝试执行以下逻辑:

 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. 上面给了我:nvarchar 04.50无法转换为数据类型int。 Which means there is something wrong with the case statement. 这意味着case语句有问题。 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: 为什么不使用不等式编写CASE

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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