dongrang2140 2016-10-18 10:55
浏览 146

sql server从两个表中获取字符串中的最大值整数

i have this sql query for mysql that works well. the query finds in 2 tables wich is the max numeric value from both and returns the value good in mysql, i have to do the same query in sql server but tells me an error syntax error near cast, expects as

the query is the next :

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

there is a way to do that the this query runs?

  • 写回答

2条回答 默认 最新

  • dongmao3131 2016-10-18 10:59
    关注

    I would start with no conversion at all:

    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 
    

    This assumes that nrt has at least 5 characters.

    If not, convert to an integer:

    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 
    

    EDIT:

    This question appears to be about MySQLs implicit conversions. I might suggestion:

    SELECT MAX(CAST(LEFT(nrt + ' ', PATINDEX(nrt, '%[^0-9]%') - 1) as int))
    . . .
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败