Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 Server子字符串引发错误“;“多重结果”;_Sql_Sql Server_Tsql - Fatal编程技术网

SQL Server子字符串引发错误“;“多重结果”;

SQL Server子字符串引发错误“;“多重结果”;,sql,sql-server,tsql,Sql,Sql Server,Tsql,我使用Microsoft SQL Server 2016 我有一个专栏名为Failover,看起来像他的: $D$Failov:12345:54362:28564 $D$Failov:12345: $D$Failov:86905:45634 我想要那个号码,所以我使用: select substring(failover, 10, 5) from dbo.f009 where failover like '$D$Failov:%' 它工作正常,但如果我想要第二列名为

我使用Microsoft SQL Server 2016

我有一个专栏名为
Failover
,看起来像他的:

  $D$Failov:12345:54362:28564      
  $D$Failov:12345:
  $D$Failov:86905:45634
我想要那个号码,所以我使用:

select substring(failover, 10, 5) 
from dbo.f009
where failover like '$D$Failov:%'
它工作正常,但如果我想要第二列名为
Account
,它会崩溃并产生多个结果

Select 
    account,
    (Select substring(failover, 10, 5) AS "1.Result"
     from dbo.f009 
     where Failover like '$D$Failov:%')
from 
    f009
where 
    Failover like '$D$Failov:%'
如何解决这个问题

有没有一个简单的方法来取第二个和第三个数字?我可以用:

substring(failover, 16, 5), substring(failover, 22, 5)

诸如此类,但我不想重复。

似乎你想要的东西可以通过一个简单的查询来实现:

Select account, substring(failover,10,5) AS "1.Result"
from dbo.f009 
where Failover like '$D$Failov:%'

您可以重复以下字符串操作:

select substring(failover, 11, 5) as num1,
       substring(failover, 17, 5) as num2,
       substring(failover, 23, 5) as num3       
from dbo.f009
where failover like '$D$Failov:%';
如果值的数量不确定,也可以将其表述为递归CTE:

with t as (
      select * from (values ('$D$Failov:12345:54362:28564'), ('$D$Failov:12345:')) v(failover)
     ),
     cte as (
      select failover, convert(varchar(max), NULL) as acct, convert(varchar(max), stuff(failover, 1, 10, '')) as rest, 0 as lev
      from t
      union all
      select failover, left(rest, 5), stuff(rest, 1, 6, ''), lev + 1
      from cte
      where rest > ':'
     )
select failover, acct, lev
from cte
where lev > 0;

是一把小提琴。

阅读,你会看到很多原因,为什么这个问题的答案绝对是肯定的!当然,但这是一张很旧的桌子,大概有30-40年了。它的设计目的是提取Cobol代码,因此我尝试将其重写为旧的SQL subselect,(subselect/subselect(subselect(subselect…))codeSystem.Data.SqlClient.SqlException(0x80131904):如果子查询不是使用EXISTS启动的,则只能在选择列表中指定一个表达式。@4nch0r是否确定?因为这里没有子查询是的,但你确实帮了我。我得到了另一个子查询,它属于代码的这一部分,可能会导致问题。我应该写,我是sry!但我试着让这条线尽可能简单。