SQL Server服务器功能中1000000到1M和1000到1K

时间:2017-05-27 06:22:41

标签: sql-server sql-server-2008

例如:

num          result
---------------------
        10    10       
       100   100   
      1000   1k   
     10000   10k   
    100000   100k   
   1000000   1M   
  10000000   10M   
 100000000   100M   
1000000000   1000M

1 个答案:

答案 0 :(得分:0)

select num, 
    case 
        when num / 1000000 >= 1 then cast(cast(num / 1000000 as varchar(10)) + 'M' as varchar(10)) 
        when num / 1000 >= 1 then cast(cast(num / 1000 as varchar(10)) + 'K' as varchar(10)) 
        else cast(num as varchar(10)) end as result
from table

输出:

+------------+--------+
|    num     | result |
+------------+--------+
|         10 | 10     |
|        100 | 100    |
|       1000 | 1K     |
|      10000 | 10K    |
|     100000 | 100K   |
|    1000000 | 1M     |
|   10000000 | 10M    |
|  100000000 | 100M   |
| 1000000000 | 1000M  |
+------------+--------+