根据nvarchar字段的前3个字符连接两个表

时间:2011-04-08 16:44:17

标签: sql sql-server-2005

我有这个简单的sql语句,但我的问题是我需要确保表1中的firstname与表2中的firstname匹配这应该很简单但是当我这样做时它会减少我的数据,我想尝试只匹配名字的前3个字符,看看是否有帮助

我想在加入部分这样做,但我不知道该怎么做

SELECT DISTINCT 
                      TOP (100) PERCENT database.dbo.NAMES.SRNAME, SUM(dbo.claim.[TOTAL PAID AMOUNT]) AS TOTAL, YEAR(GETDATE()) 
                      - YEAR(CONVERT(datetime, CAST(database.dbo.NAMES.BORN - 2 AS int))) AS AGE1, dbo.claim.[BOOKING #], CONVERT(datetime, 
                      CAST(database.dbo.NAMES.BORN - 2 AS int)) AS DOB, dbo.claim.[PREMIUM YEAR]
FROM         database.dbo.NAMES INNER JOIN
                      dbo.claim ON database.dbo.NAMES.BOOKNO = dbo.claim.[BOOKING #] AND 
                      database.dbo.NAMES.FIRST = dbo.claim.[FIRST NAME]
WHERE     (dbo.claim.[PLAN #] = 'F432C' OR
                      dbo.claim.[PLAN #] = '00000F432C') AND (isnumeric(dbo.claim.[BOOKING #]) = 1)
GROUP BY database.dbo.NAMES.SRNAME, database.dbo.NAMES.BORN, dbo.claim.[BOOKING #], dbo.claim.[PREMIUM YEAR]
ORDER BY dbo.claim.[BOOKING #]

1 个答案:

答案 0 :(得分:3)

Join t1 to t2 on LEFT(t1.fname, 3) = LEFT(t2.fname, 3)