我需要将Postgres特定的查询转换为SQL Server(T-SQL)

时间:2017-03-17 08:23:47

标签: sql sql-server postgresql

我在postgres上运行了一个查询,我需要转换它,因为它应该在MS-SQL Server上运行。

case when position('.' in campo30) >= 1
     then CAST(replace(replace(CAMPO30,'.',''),',','.') AS FLOAT)
     else CAST(replace(CAMPO30,',','.') AS FLOAT)
end

MS-SQL Server的等效代码是什么?

1 个答案:

答案 0 :(得分:0)

在SQL-Server上使用CHARINDEX

CHARINDEX(expressionToFind,expressionToSearch [,start_location])

DECLARE @CAMPO30 varchar(64) = '2,663.25';

SELECT CASE WHEN CHARINDEX('.', @CAMPO30) > 1
            THEN CAST(REPLACE(REPLACE(@CAMPO30,'.',''),',','.') AS FLOAT)
            ELSE CAST(REPLACE(@CAMPO30,',','.') AS FLOAT)
       END AS CAMPO30
GO

| CAMPO30 |
| ------: |
| 2.66325 |

dbfiddle here