Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询以替换字符串并将字符串的其余部分转换为Int_Sql_Sql Server - Fatal编程技术网

SQL查询以替换字符串并将字符串的其余部分转换为Int

SQL查询以替换字符串并将字符串的其余部分转换为Int,sql,sql-server,Sql,Sql Server,我有以下问题。此查询应该使用where参数查看my accounts表。这将返回所有以前导3个字母“DAG”开头的帐户代码。数据返回的格式一致,前面是三个字母,后面是一个数字。然后我想得到最大的数字。为此,我通过将字符串转换为Int来对其排序 我得到以下错误: 味精207,16级,状态1,第24行 列名“AccountCodeWithoutDAG”无效 这是我的SQL查询 SELECT TOP 1 REPLACE(AccountCode, 'DAG', '') AS AccountCo

我有以下问题。此查询应该使用where参数查看my accounts表。这将返回所有以前导3个字母“DAG”开头的帐户代码。数据返回的格式一致,前面是三个字母,后面是一个数字。然后我想得到最大的数字。为此,我通过将字符串转换为Int来对其排序

我得到以下错误:

味精207,16级,状态1,第24行
列名“AccountCodeWithoutDAG”无效

这是我的SQL查询

SELECT TOP 1 
    REPLACE(AccountCode, 'DAG', '') AS AccountCodeWithoutDAG 
FROM 
    Accounts 
WHERE
    MangCode = 'ADFG'  
ORDER BY
    CONVERT(INT, AccountCodeWithoutDAG)

我做错了什么?

问题是AccountCodeWithout DAG,这是一个别名,不能在订单中使用 看看我在下面的测试中所做的,在orderby的convert部分使用replace语句

declare @AccountCode varchar(100)='DAG123456'

SELECT  top 1 REPLACE(@AccountCode,'DAG','') AS AccountCodeWithoutDAG 
   Order by Convert(int, REPLACE(@AccountCode,'DAG','')) 

问题是AccountCodeWithoutDAG,这是一个别名,无法在订单中使用 看看我在下面的测试中所做的,在orderby的convert部分使用replace语句

declare @AccountCode varchar(100)='DAG123456'

SELECT  top 1 REPLACE(@AccountCode,'DAG','') AS AccountCodeWithoutDAG 
   Order by Convert(int, REPLACE(@AccountCode,'DAG','')) 

您不能将
order by
convert
别名一起使用

但是您可以尝试使用子查询来获取
AccountCodeWithoutDAG
然后通过
orderby
it

SELECT  TOP 1 AccountCodeWithoutDAG 
FROM 
(
    SELECT REPLACE(AccountCode,'DAG','') AS AccountCodeWithoutDAG
    FROM Accounts 
    where  MangCode = 'ADFG'  
) t1
Order by  Convert(int, AccountCodeWithoutDAG )

您不能将
order by
convert
别名一起使用

但是您可以尝试使用子查询来获取
AccountCodeWithoutDAG
然后通过
orderby
it

SELECT  TOP 1 AccountCodeWithoutDAG 
FROM 
(
    SELECT REPLACE(AccountCode,'DAG','') AS AccountCodeWithoutDAG
    FROM Accounts 
    where  MangCode = 'ADFG'  
) t1
Order by  Convert(int, AccountCodeWithoutDAG )

正如其他人所指出的,在
select
中为计算值指定一个别名并不能使您在后续子句中使用相同的别名

在sql server中实现这一点的方法是
交叉应用

SELECT TOP 1 AccountCodeWithoutDAG
FROM Accounts 
cross apply (select REPLACE(AccountCode,'DAG','') AS AccountCodeWithoutDAG ) as calcquery
where 
   MangCode = 'ADFG'  
   Order by Convert(int, AccountCodeWithoutDAG )

正如其他人所指出的,在
select
中为计算值指定一个别名并不能使您在后续子句中使用相同的别名

在sql server中实现这一点的方法是
交叉应用

SELECT TOP 1 AccountCodeWithoutDAG
FROM Accounts 
cross apply (select REPLACE(AccountCode,'DAG','') AS AccountCodeWithoutDAG ) as calcquery
where 
   MangCode = 'ADFG'  
   Order by Convert(int, AccountCodeWithoutDAG )

选择从…起哪里订购人…从哪里来抱歉那是我的坏拷贝粘贴它。我编辑了它的选择。。。从…起哪里订购人…从哪里来抱歉那是我的坏拷贝粘贴它。我在SELECT语句之后通过运行编辑了itORDER,因此别名为visible@AjanBalakumaran完全取决于他使用的sql server版本。您描述的是在SELECT语句之后运行的一个相当新的featureORDER by,因此别名为visible@AjanBalakumaran完全取决于他使用的sql server版本。您所描述的是一个相当新的特性,我很好奇这在select和orderby中重复转换时是如何执行的。用另一种方式做会让人感觉更干净我很好奇,在select和orderby中重复转换时,它的性能如何。用另一种方式做会让你感觉更干净你一定很喜欢十字架!很多很棒的用途!你一定喜欢十字架!很多很棒的用途!