我收到错误“将nvarchar值'23,24,3,45,91'转换为数据类型int时转换失败。” ON子句似乎发生了错误。 E.ID是整数字段,而F.LegalIssue是由逗号分隔的整数的varchar字段。下面是带有该错误的代码。
SELECT F.[FDTitle], E.PrimaryOpID as [FD Primary OP ID], F.County as [FD County], F.Status as [FD Status], F.IssueDate as [FD Date]
FROM [dbo].[tbl_FinalDetMain] F
LEFT OUTER JOIN [dbo].[tbl_lk_Exemptions_FD] E ON E.ID = F.LegalIssue
WHERE F.[FDNbr] = '2013-0041'
我已经尝试了下面的代码用于on子句,但它只返回一个整数值,而不是整个整数字符串。
E.ID = cast(LEFT(F.LegalIssue,PATINDEX('%[^0-9]%',F.LegalIssue)-1) as int)
结果应包括由逗号分隔的五个整数。
答案 0 :(得分:8)
如果LegalIssue
包含逗号分隔的数字字符串,那么您真的需要一个关联表。缺乏这一点,这是一种方便(但不是有效)的方式来进行连接:
SELECT F.[FDTitle], E.PrimaryOpID as [FD Primary OP ID], F.County as [FD County],
F.Status as [FD Status], F.IssueDate as [FD Date]
FROM [dbo].[tbl_FinalDetMain] F LEFT OUTER JOIN
[dbo].[tbl_lk_Exemptions_FD] E
ON ','+F.LegalIssue+',' like '%,'cast(E.ID as varchar(255))+',%'
WHERE F.[FDNbr] = '2013-0041';
这会以逗号为前缀和后缀,以避免冲突,例如在“1,100,1000”中找到“10”。
答案 1 :(得分:2)
使用xml数据类型,您可以将字符串分解为这样的整数。用户定义函数的良好候选者我会说: - )
declare @test varchar(max)
set @test = '1,2,3,4,5'
select
T2.item.value('(./text())[1]','int')
from
(select convert(xml,'<items><t>'+replace(@test,',','</t><t>')+'</t></items>') as xmldoc)
as xmltable
CROSS APPLY xmltable.xmldoc.nodes('/items/t') as T2(item)
答案 2 :(得分:1)
您必须将F.LegalIssue标准化为多行,否则您将不得不使用LIKE
像
这样的东西 CAST(E.ID A VARCHAR(50)) = F.LegalIssue
OR F.LegalIssue LIKE CAST(E.ID A VARCHAR(50)) + ',%'
OR '%,' + F.LegalIssue LIKE CAST(E.ID A VARCHAR(50)) + ',%'
OR '%,' + F.LegalIssue LIKE CAST(E.ID A VARCHAR(50))
如您所见,表的实际设计是个问题。您应该避免使用当前的设计,并选择1 to many
或many to many
设计。
以下是如何使用递归CTE展平值的演示
创建测试表和数据
CREATE TABLE Tada(
ID INT,
SomeCommaString VARCHAR(50)
)
INSERT INTO Tada Values (1, '10'),(2,'5,6,12,16')
展平桌子
;WITH Vals AS (
SELECT
ID,
CASE
WHEN CHARINDEX(',',SomeCommaString) = 0
THEN SomeCommaString
WHEN CHARINDEX(',',SomeCommaString) > 0
THEN LEFT(SomeCommaString,CHARINDEX(',',SomeCommaString) - 1)
END Val,
CASE
WHEN CHARINDEX(',',SomeCommaString) > 0
THEN RIGHT(SomeCommaString,LEN(SomeCommaString) - CHARINDEX(',',SomeCommaString))
ELSE NULL
END Remainder
FROM Tada
UNION ALL
SELECT
ID,
CASE
WHEN CHARINDEX(',',Remainder) = 0
THEN Remainder
WHEN CHARINDEX(',',Remainder) > 0
THEN LEFT(Remainder,CHARINDEX(',',Remainder) - 1)
END Val,
CASE
WHEN CHARINDEX(',',Remainder) > 0
THEN RIGHT(Remainder,LEN(Remainder) - CHARINDEX(',',Remainder))
ELSE NULL
END Remainder
FROM Vals
WHERE Remainder IS NOT NULL
)
SELECT ID, Val
FROM Vals