0

我正在构建一个临时表是sqlserver。现在在这个临时表中,我想检查一列整数值是否存在于另一列的文本字段中。

例如。我有上校。天,其值为 2,10,15,30... 等多次,并且对于每个值,另一列具有完整的文本,其中包含一组规则的描述,最后,时间线为 2日历日或 30 个日历日或 10 个工作日,应与整数列匹配。

如何比较规则文本列中文本匹配中的 int 值?

例如。

col1   col2
2      ....should happen....- 2 business days
4      ....should happen....- 4 business days
5      ....should happen....- 5 business days
6      ....should happen....- 6 business days
15     ....should happen....- 15 business days
30     ....should happen....- 30 business days
4

3 回答 3

2

您可以从字符串中过滤掉 int ,如下所示。(基于几个假设only one '-' in the string before the number, number has left and right spaces:)

declare @s varchar(100) = '...should happen....- 20 business days'

;with cte as (
    select right(@s,len(@s)-charindex('-',@s,0)- 1) as rightText
)
select left(rightText, charindex(' ', rightText,0))
from cte

查询就像

;with cte as (
    select col1, col2,
           right(col2,len(col2)-charindex('-',col2,0)- 1) as rightText

    from yourTable
 )
 select col1,col2
 from cte
 where left(rightText, charindex(' ', rightText,0)) = col1
于 2013-09-06T15:37:42.650 回答
1
SELECT *
FROM TEMP
WHERE col2 LIKE '%- '+cast(col1 as varchar)+' % days'

请参阅SQLFIDDLE

或者可能 :

SELECT *,
       CASE WHEN col2 LIKE '%- '+cast(col1 as varchar)+' % days' 
            THEN 'Exists' 
            ELSE 'Not Exists' END AS "Exists"
FROM TEMP

请参阅SQLFIDDLE

对于 msi77 :

结果

| COL1 |                                    COL2 |     EXISTS |
|------|-----------------------------------------|------------|
|    2 |  ....should happen....- 2 calendar days |     Exists |
|    2 | ....should happen....- 20 calendar days | Not Exists |
|    4 |  ....should happen....- 4 calendar days |     Exists |
|    5 |  ....should happen....- 5 business days |     Exists |
|    6 |  ....should happen....- 6 business days |     Exists |
|   15 | ....should happen....- 15 business days |     Exists |
|  999 | ....should happen....- 00 business days | Not Exists |
|   30 | ....should happen....- 30 business days |     Exists |
于 2013-09-06T15:45:04.870 回答
0

首先,您可以像这样从 varchar 中获取数字:

SUBSTRING(col2, CHARINDEX('-', col2) + 1, 2)

然后您可以将其转换为 INT ,如下所示:

CONVERT(INT, SUBSTRING(col2, CHARINDEX('-') + 1, 2))

子串

CHARINDEX

兑换

于 2013-09-06T15:30:40.373 回答