MS SQL Server View not allowing CONCAT

非 Y 不嫁゛ 提交于 2020-04-07 08:00:06

问题


MS SQL Server 2014.

I have a SQL statement that works fine:

SELECT CONCAT (
        CAST(T1.[F1] AS INTEGER),
        CAST(T1.[F2] AS INTEGER)
        ) AS F3 
FROM mytable AS T1

If I then put this into a view, and try to run I receive the error:

Operand data type int is invalid for concat operator

F1 and F2 both contain decimals but I want them concatenating e.g.:

F1 = 123.00000
F2 = 456.00000

Therefore F3 = 123456

Why does the view not allow this and is there a solution?


回答1:


Don't use the visual designers.

They are buggy and in this case convert

    SELECT CONCAT (
        CAST(T1.[F1] AS INTEGER),
        CAST(T1.[F2] AS INTEGER)
        ) AS F3 
FROM mytable AS T1

to

SELECT { fn CONCAT(CAST(T1.F1 AS INTEGER), CAST(T1.F2 AS INTEGER)) } AS F3
FROM     mytable AS T1

Calling the highly limited ODBC escape sequence version of the function (that only accepts two parameters which must be strings)

Just use a standard new query window to execute your CREATE VIEW/ALTER VIEW/SELECT FROM operations and you won't get this issue.




回答2:


Please use concat function like below

SELECT {fn CONCAT('123 ', '456') }



来源:https://stackoverflow.com/questions/60509677/ms-sql-server-view-not-allowing-concat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!