String 在sql server中使用大小写匹配字符串?
我试图在一个SQL Select语句中使用String 在sql server中使用大小写匹配字符串?,string,tsql,String,Tsql,我试图在一个SQL Select语句中使用CASE,该语句将允许我获得结果,其中我可以利用一个字符串的长度生成另一个字符串的结果。这些用于来自两个数据集的不匹配记录,这两个数据集共享一个公共ID,但数据源不同 案件陈述如下: Select Column1, Column2, Case When Column1 = 'Something" and Len(Column2) = '35' Then Column1 = "Something Else" and substring(Column2,
CASE
,该语句将允许我获得结果,其中我可以利用一个字符串的长度生成另一个字符串的结果。这些用于来自两个数据集的不匹配记录,这两个数据集共享一个公共ID,但数据源不同
案件陈述如下:
Select Column1, Column2,
Case
When Column1 = 'Something" and Len(Column2) = '35' Then Column1 = "Something Else" and substring(Column2, 1, 35)
End as Column3
From dbo.xxx
当我运行它时,我得到以下错误:
Msg 102,15级,状态1,第5行“=”附近的语法不正确
更新您的查询
希望这有帮助 当时,您需要为每个
都有一个值,并且应该有一个否则
:
Select Data_Source, CustomerID,
CASE
WHEN Data_Source = 'Test1' and Len(CustomerName) = 35 THEN 'First Value'
WHEN Data_Source = 'Test2' THEN substring(CustomerName, 1, 35)
ELSE 'Sorry, no match.'
END AS CustomerName
From dbo.xx
仅供参考:Len()
不返回字符串
编辑:
解决某些注释的SQL Server答案可能是:
declare @DataSource as Table ( Id Int Identity, CustomerName VarChar(64) )
declare @VariantDataSource as Table ( Id Int Identity, CostumerName VarChar(64) )
insert into @DataSource ( CustomerName ) values ( 'Alice B.' ), ( 'Bob C.' ), ( 'Charles D.' )
insert into @VariantDataSource ( CostumerName ) values ( 'Blush' ), ( 'Dye' ), ( 'Pancake Base' )
select *,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs are not handled gracefully.
Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) ) as Clustermere,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs in CustomerName are explicitly handled.
case
when CustomerName is NULL then ''
when Len( CustomerName ) > Len( CostumerName ) then Substring( CostumerName, 1, Len( CustomerName ) )
else Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) )
end as 'Crustymore'
from @DataSource as DS inner join
@VariantDataSource as VDS on VDS.Id = DS.Id
这种SQL毫无意义。您能给出一些示例数据并说明您预期会发生什么吗?这就是确切的sql:选择data\u Source、CustomerID、CASE WHEN data\u Source='Test1'和Len(CustomerName)='35'然后Data_Source='Test2'和子字符串(CustomerName,1,35)作为dbo中的CustomerName结束。xxData_Source CustomerID CustomerName Test xxx xxx PLC(LONDON BR Test1 xxx xxx PLC(LONDON BR2)ELSE
部分在Transact-SQL情况下不是强制性的(如果这是您的意思)。@AndriyM-这是“最佳实践”建议。我并没有试图使用multiple,但在我的语句中,意图是在ID相似但数据源(Column1)的记录中操作数据是variant,因此返回的长度是相同的。当然,添加ELSE是教科书,但不能分散我的注意力。有人知道为什么在my THEN之后添加“=”时会出现错误吗?或者提供了一个解决方案,允许我以这种方式使用。任何其他建议都非常感谢。@user1368436-As我试着解释一下,THEN
提供了一个值,作为大小写
表达式的值返回。您不能在THEN
中赋值。好的,那么您有什么建议吗?我需要处理ID相似但数据源(第1列)的记录中的数据variant是否返回相同的长度?嗨,Seanbun,谢谢你的建议。我不想在这里连接任何数据。处理数据时需要长度,以便返回值具有相同的长度。我想了解为什么“=”在我的THEN之后会导致错误,或者我可以如何使用建议来方便记录相同ID、变量数据源以匹配特定字段的返回长度。“Else”是否有帮助?如下面所示,或者您是否可以声明一个变量@Result来存储您的值并在末尾返回?选择Column1、Column2,当Column1='Something'和Len(Column2)=35时,选择Column1、Column2、Case,然后选择'Something other'+子字符串(第2、1、35列)否则第1列将作为dbo.xxx中的第3列结束
declare @DataSource as Table ( Id Int Identity, CustomerName VarChar(64) )
declare @VariantDataSource as Table ( Id Int Identity, CostumerName VarChar(64) )
insert into @DataSource ( CustomerName ) values ( 'Alice B.' ), ( 'Bob C.' ), ( 'Charles D.' )
insert into @VariantDataSource ( CostumerName ) values ( 'Blush' ), ( 'Dye' ), ( 'Pancake Base' )
select *,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs are not handled gracefully.
Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) ) as Clustermere,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs in CustomerName are explicitly handled.
case
when CustomerName is NULL then ''
when Len( CustomerName ) > Len( CostumerName ) then Substring( CostumerName, 1, Len( CustomerName ) )
else Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) )
end as 'Crustymore'
from @DataSource as DS inner join
@VariantDataSource as VDS on VDS.Id = DS.Id