我有这个mysql的SQL查询,运行良好。查询在2个表中找到两个表中的最大数值并返回mysql中的值,我必须在sql server中执行相同的查询但是告诉我错误语法错误接近强制转换,期望为
查询是下一个:
SELECT MAX(CAST(RIGHT(nrt, 5) AS UNSIGNED))
FROM
(
SELECT nrt from asp where nrt != ' ' and nrt is not null
UNION ALL
SELECT nrt from asp_historic where nrt != ' ' and nrt is not null
) as subQuery
有一种方法可以执行此查询吗?
答案 0 :(得分:0)
我会从没有转换开始:
SELECT MAX(RIGHT(nrt, 5)) FROM asp WHERE nrt <> ' ' and nrt is not null
UNION ALL
SELECT nrt FROM asp_historic WHERE nrt M< ' ' and nrt is not null
这假定nrt
至少有5个字符。
如果不是,请转换为整数:
SELECT CAST(MAX(RIGHT(nrt, 5)) as int) FROM asp WHERE nrt <> ' ' and nrt is not null
UNION ALL
SELECT nrt FROM asp_historic WHERE nrt <> ' ' and nrt is not null
编辑:
这个问题似乎是关于MySQL的隐式转换。我可能会建议:
SELECT MAX(CAST(LEFT(nrt + ' ', PATINDEX(nrt, '%[^0-9]%') - 1) as int))
. . .
答案 1 :(得分:0)
以下是 SQL Server ...
的相同代码SELECT MAX(RIGHT(ABS(nrt), 5))
FROM
(
SELECT nrt from asp where nrt <> ' ' and nrt is not null
UNION ALL
SELECT nrt from asp_historic where nrt <> ' ' and nrt is not null
) as subQuery
您在寻找nrt != ' ' and nrt is not null
吗?
我认为它应该是OR
条件而不是AND
。