如何从逗号分隔的varchar列SQL中查找int值

时间:2016-06-30 10:46:35

标签: sql select

我有3张桌子。事务,LineItems是Transactions表的子项,以及拒绝

目前在lineitems表中有一个名为rejectioncodes的列,它包含拒绝表的id值,但是以逗号分隔的字符串值。

enter image description here

我如何能够选择id在该varchar中的行?

到目前为止我的代码示例

select transactions.fTransactionID 
from Transactions inner join
     LineItems
     on transactions.fTransactionID = lineitems.fTransactionID 
where fTransactionStatusID = 11 and
      LineItems.fRejectionCodes in (select fRejectionCodeID
                                    from RejectionCodes 
                                    where fRejectionCodeID in (1,7,8,9,12,13,15)
                                   _

当然我的查询会说由于列是varchar而无法完成

2 个答案:

答案 0 :(得分:1)

如评论所述,设计更改将是一个好主意,但对于逗号分隔的问题,可以在下面的链接中更好地解释:

http://www.databasejournal.com/features/mssql/converting-comma-separated-value-to-rows-and-vice-versa-in-sql-server.html

您可以使用此功能将逗号分隔的数据分解为行,然后对其进行处理

答案 1 :(得分:0)

Use STUFF() with FOR XML PATH('')as follows Which gives you the same comma seperated result.


SELECT  STUFF((SELECT  ',' + INSTITUTIONNAME
            FROM EDUCATION EE
            WHERE  EE.STUDENTNUMBER=E.STUDENTNUMBER
            ORDER BY sortOrder
        FOR XML PATH('')), 1, 1, '') AS listStr

FROM EDUCATION E
GROUP BY E.STUDENTNUMBER