列数据类型Varchar的百分比

时间:2018-01-12 14:10:06

标签: sql-server

我想以百分比显示数字,如下所示,列数据类型为Varchar

1       100.0% 
0.8      80.0% 
0.9      90.0% 

2 个答案:

答案 0 :(得分:0)

示例数据

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:BEG::END

我们不应该以字符串格式保存数值数据,如果要以表格百分比获取数据,首先将数字列数据从varchar转换为十进制数据类型并乘以100.请尝试以下

DECLARE @numberdata AS TABLE ( Number VARCHAR(100))
INSERT INTO @numberdata
SELECT 1   UNION ALL     
SELECT 0.8 UNION ALL    
SELECT 0.9

SELECT * FROM @numberdata

结果

SELECT  Number,
CAST(CAST(Number AS DECIMAL(4,1))*100 AS VARCHAR(10))+'%' AS InPercantage FROM @numberdata

答案 1 :(得分:0)

我的方法是:

  • 创建CTE
  • 将其转换为数字数据类型,如十进制
  • 将该值乘以100
  • 将值转换回varchar并添加百分号

    <div>
      <label>
        One:
        <input type="text">
      </label>
    </div>
    <div>
      <label>
        Two:
        <input type="text">
      </label>
    </div>
    <div>
      <label>
        Three:
        <select>
          <option>ay</option>
          <option>bee</option>
          <option>see</option>
        </select>
      </label>
    </div>

原始数据:

with tab as
( Select (cast(column1 as decimal(9,1)) *100) as 'newcolumn'
From sample1)

Select cast(newcolumn as varchar(12)) + '%' as 'Percent'
From tab

输出:

Select * from sample1
column1
1
0.5
0.2