【发布时间】:2015-10-07 22:10:25
【问题描述】:
我有一个 TSQL 语句,它查询单个表以查找重复的街道地址号码。例如,“123 Street”匹配“123 St.”。我使用 CHARINDEX 通过选择字符串左侧但在空格之前的字符来分隔字符串,这些字符几乎总是如下所示:
"SELECT NewId() as NewId," +
//We rename the dbo.User table as "a" then rename it again as "b" so we can look for duplicate Street Address numbers
"a.Id AS LeftID,a.DateSubmitted AS LeftDateSubmitted,a.Updated AS LeftUpdated," +
"a.Status AS LeftStatus,a.StreetAddress AS LeftStreetAddress," +
"b.Id AS RightID,b.DateSubmitted AS RightDateSubmitted,b.Updated AS RightUpdated," +
"b.Status AS RightStatus,b.StreetAddress AS RightStreetAddress " +
//We join the 2 virtual dbo.User tables where table b Id's are greater than table a meaning b records are newer
"FROM [User] a JOIN [User] b ON b.Id > a.Id AND " +
//LEFT selects the left most characters (usually numbers) in the StreetAddress field string before the space ' '
//and eliminates the rest of the address isolating just the street address numbers for matching
"LEFT(a.StreetAddress,CHARINDEX(' ',a.StreetAddress)) = LEFT(b.StreetAddress,CHARINDEX(' ',b.StreetAddress)) " +
//Don't show orange or blue status records
"AND b.Status != 'Orange' AND a.Status != 'Orange' AND a.Status != 'Blue' AND b.Status != 'Blue' " +
//If a b record (newer) is red then ignore because it is completed and ignore a records (oldest) older than 90 days
"WHERE a.DateSubmitted >= (GetDate() - 90) AND b.Status != 'Red' " +
//Show newest records first
"ORDER BY b.DateSubmitted DESC"
这一直运作得相当好,直到今天我们注意到如果该人以所有大写字母输入地址,则会出现误报,如下所示:
我的使用理解:
LEFT(a.StreetAddress,CHARINDEX(' ',a.StreetAddress))
在用于我的 JOIN 的空间之前会导致最左边的字符,但上图显示不应该发生的匹配?请帮助 SQL 初学者...
【问题讨论】:
标签: c# sql-server tsql azure