Sql server 比较两列(一个varchar,一个int)
我正在用sqlserver构建一个临时表。现在在这个临时表中,我想检查一列整数值是否存在于另一列的文本字段中 例如,我有col.days,其值为2,10,15,30。。。因此,对于每个值,一列重复多次,另一列包含完整文本,其中包含一组规则的描述,最后,时间线是2个日历日或30个日历日或10个工作日,应该与整数列匹配 如何比较规则文本列中文本匹配中的int值 例如Sql server 比较两列(一个varchar,一个int),sql-server,Sql Server,我正在用sqlserver构建一个临时表。现在在这个临时表中,我想检查一列整数值是否存在于另一列的文本字段中 例如,我有col.days,其值为2,10,15,30。。。因此,对于每个值,一列重复多次,另一列包含完整文本,其中包含一组规则的描述,最后,时间线是2个日历日或30个日历日或10个工作日,应该与整数列匹配 如何比较规则文本列中文本匹配中的int值 例如 col1 col2 2 ....should happen....- 2 business days 4 ..
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
首先,您可以从varchar中获得如下数字:
SUBSTRING(col2, CHARINDEX('-', col2) + 1, 2)
CONVERT(INT, SUBSTRING(col2, CHARINDEX('-') + 1, 2))
然后可以将其转换为INT,如下所示:
SUBSTRING(col2, CHARINDEX('-', col2) + 1, 2)
CONVERT(INT, SUBSTRING(col2, CHARINDEX('-') + 1, 2))
首先,您可以从varchar中获取数字,如下所示:
SUBSTRING(col2, CHARINDEX('-', col2) + 1, 2)
CONVERT(INT, SUBSTRING(col2, CHARINDEX('-') + 1, 2))
然后可以将其转换为INT,如下所示:
SUBSTRING(col2, CHARINDEX('-', col2) + 1, 2)
CONVERT(INT, SUBSTRING(col2, CHARINDEX('-') + 1, 2))
您可以从字符串中过滤出int,如下所示。(基于一些假设:
数字前的字符串中只有一个“-”,数字有左空格和右空格
)
我会是这样的
;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
您可以从字符串中过滤出int,如下所示。(基于一些假设:
数字前的字符串中只有一个“-”,数字有左空格和右空格
)
我会是这样的
;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
看
或可能是:
SELECT *,
CASE WHEN col2 LIKE '%- '+cast(col1 as varchar)+' % days'
THEN 'Exists'
ELSE 'Not Exists' END AS "Exists"
FROM TEMP
看
对于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 |
看
或可能是:
SELECT *,
CASE WHEN col2 LIKE '%- '+cast(col1 as varchar)+' % days'
THEN 'Exists'
ELSE 'Not Exists' END AS "Exists"
FROM TEMP
看
对于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 |
并且
选择LEFT(RIGHT(col2,13),2)作为col2,col1 FROM tentable
col2是否总是以结尾。。。营业日
?数字前的-
是否总是存在,并且是唯一的破折号?@aaron bertrand:the。。。。只是一个表达式,没有点,只有“是”,文本结束后,格式为“-2个工作日”或“-1个工作日”,依此类推,选择LEFT(RIGHT(col2,13),2)作为col2,col1 FROM tentable
col2是否总是以结尾。。。营业日
?数字前的-
是否总是存在,并且是唯一的破折号?@aaron bertrand:the。。。。只是一个表达式,没有点,只有“是”,文本结束后,格式为“-2个工作日”或“-1个工作日”,因此设置错误:Msg 189,级别15,状态1,第1065行charindex函数需要2到3个参数。抱歉,忘了在其中添加“col2”作为第二个参数。现在应该已修复。获取错误:Msg 189,级别15,状态1,第1065行charindex函数需要2到3个参数。抱歉,忘记在其中添加“col2”作为第二个参数。现在应该修好了。@msi77绝对不是问题……看,有些东西已经改变了。OK@msi77绝对不是问题…看,有些东西改变了。好啊