将两种类型的转换添加到单列中

时间:2013-04-17 11:29:11

标签: sql sql-server sql-server-2008 casting

我的表中有一个名为salary的列。另一个表的值存储如

  • 5万12千同样..

我想要结果

  • 5 * 10000 + 12 * 1000

我创建了一个查询

SELECT TOP(10) 'INSERT INTO jobs(Budget) VALUES('+
CAST(SUBSTRING(
      CAST(r.Budget AS VARCHAR(50)), 0, PATINDEX('%laks%', r.Budget))*100000 AS VARCHAR(50))
      + ',' +CAST(SUBSTRING(
      CAST(r.Budget AS VARCHAR(50)), PATINDEX('%laks%', r.Budget) + 4
    ,patindex('%Thousands%', r.Budget) - PATINDEX('%laks%', r.Budget) - 4)* 1000  AS VARCHAR(50))+')'
    FROM requirementsdetailsfororganization r

在这里,我可以将单独的值相乘。我不能添加两个值。

当我使用上述查询时,我的结果是

INSERT INTO jobs(Budget) VALUES(200000,5000)

预期输出:

INSERT INTO jobs(Budget) VALUES(205000)

1 个答案:

答案 0 :(得分:5)

SELECT TOP(10) 'INSERT INTO jobs(Budget) VALUES('+
        CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), 0, PATINDEX('%laks%', r.Budget))*100000
            + SUBSTRING(CAST(r.Budget AS VARCHAR(50)), PATINDEX('%laks%', r.Budget) + 4, 
                                                       patindex('%Thousands%', r.Budget) - PATINDEX('%laks%', r.Budget) - 4)* 1000 AS VARCHAR(50))+')'
    FROM requirementsdetailsfororganization r