将两个列值与SQL Server中的正则表达式进行比较

时间:2016-09-08 19:28:15

标签: sql-server regex

我正在尝试比较两个独立表中的两列(表A,表B)。 B中的列等于表A中的列,之后可能有额外的数字。

示例:

A.Col1 = 95792313

B.Col1 = 9579231300

Which means that B.Col1 = '%' + A.Col +'%'
A有大约300万条记录,B有1500万条记录。我如何在SQL中使用正则表达式来实现它?

2 个答案:

答案 0 :(得分:2)

这样的事情怎么样......

declare @a bigint = 95792313
declare @b bigint = 9579231300

select 1 where @a like left(@b,len(@a))

我只是将B.Col1(在这种情况下为@b)截断为A.Col1的长度,在这种情况下为@a

所以对你来说,像是

WHERE A.COL1 LIKE LEFT(B.Col1,LEN(A.Col1))

答案 1 :(得分:1)

由于您比较这些值的方法不是数字值,而是基于字符的,最简单的方法是仅将org.onosproject.restA.Col1开头相同长度的片段进行比较

试试这个:

B.Col1

结果

DECLARE @tblA TABLE(Col1 BIGINT);
DECLARE @tblB TABLE(Col1 BIGINT);

INSERT INTO @tblA VALUES(123),(1234),(12345);
INSERT INTO @tblB VALUES(12300),(12340000),(1345);

SELECT A.Col1
      ,B.Col1
      ,LEN(A.Col1)
      ,CASE WHEN A.Col1=LEFT(B.Col1,LEN(A.Col1)) THEN 'Start with the same digits' ELSE '' END
FROM @tblA AS A
CROSS JOIN @tblB AS B

更新

两个表中有数百万行的+----------+----------+--------------------+----------------------------+ | Col1 | Col1 | (Kein Spaltenname) | (Kein Spaltenname) | +----------+----------+--------------------+----------------------------+ | 123 | 12300 | 3 | Start with the same digits | +----------+----------+--------------------+----------------------------+ | 1234 | 12300 | 4 | | +----------+----------+--------------------+----------------------------+ | 12345000 | 12300 | 8 | | +----------+----------+--------------------+----------------------------+ | 123 | 12340000 | 3 | Start with the same digits | +----------+----------+--------------------+----------------------------+ | 1234 | 12340000 | 4 | Start with the same digits | +----------+----------+--------------------+----------------------------+ | 12345000 | 12340000 | 8 | | +----------+----------+--------------------+----------------------------+ | 123 | 1345 | 3 | | +----------+----------+--------------------+----------------------------+ | 1234 | 1345 | 4 | | +----------+----------+--------------------+----------------------------+ | 12345000 | 1345 | 8 | | +----------+----------+--------------------+----------------------------+ 并不是一个好主意。这只是为了说明这种方法。您可以使用CROSS JOIN并将此代码作为加入条件:

INNER JOIN