如何在两个空间sql server之后获取PostCode的最后两个数据

时间:2016-07-12 06:58:20

标签: sql-server sql-server-2008 sql-server-2008-r2

我有以下查询我只想要突出显示的帖子代码如何得到这个请帮助我我想要这样的结果Via1:PostCode,Via2:PostCode,Via3:PostCode

SELECT    
  STUFF((  
    SELECT  ' , Via ' + CAST(ROW_NUMBER() OVER (ORDER BY  t1.BookingId asc) AS VARCHAR(4))+  
           ' : ' +  t2.ViaLocValue    
    FROM Booking_ViaLocations t2  
    WHERE t2.BookingId = t1.BookingId  
    FOR XML PATH (''))  
  ,1,2,'') AS Names  
FROM Booking_ViaLocations t1  
WHERE t1.BookingId = b.Id  
GROUP BY t1.BookingId

请看下面的图片我有更多的点通过点地址,我希望得到突出显示的邮件地址任何人帮助我,我非常感谢你

enter image description here

我正在尝试此查询,但其显示错误无效长度参数传递给RIGHT函数。

CREATE TABLE booking
(
ID int IDENTITY(1,1) PRIMARY KEY,
address varchar(255) 

)

insert into booking values('NORTHWICK AVENUE HARROW HA3 0AB')
insert into booking values('CHANCERY LANE LONDON WC2A 1AB')

CREATE TABLE Booking_ViaLocations
(
ID int IDENTITY(1,1) PRIMARY KEY,
BookingId  int,
ViaLocations varchar(255) 

)

insert into Booking_ViaLocations values (1,'NORTHWICK AVENUE HARROW HA3 0AB')
insert into Booking_ViaLocations values (1,'CHANCERY LANE LONDON WC2A 1AB')
insert into Booking_ViaLocations values (1,'NEW OXFORD STREET LONDON WC1A 1BA')



 SELECT    
      STUFF(  (  
                SELECT  ' , Via ' 
                        + CAST(ROW_NUMBER() OVER 
                               (ORDER BY  t1.BookingId asc) AS VARCHAR(4)) 
                        + ' : ' +  
         right(t2.ViaLocValue, 
         charindex(' ', reverse(t2.ViaLocValue), 
         charindex(' ', reverse(t2.ViaLocValue)) + 1) - 1)
                FROM  Booking_ViaLocations t2  
                WHERE t2.BookingId = t1.BookingId  
                FOR XML PATH ('')
               )  
              ,1,2,'') AS Names  
  FROM   Booking_ViaLocations t1  

  inner join Booking b on
 t1.BookingId = b.Id  
GROUP BY t1.BookingId

2 个答案:

答案 0 :(得分:2)

获取字符串的最后2个字,在反向字符串上使用charindex()两次

declare @str varchar(100) = 'MERTON RD HARROW HA2 0AA'

select  right(@str, charindex(' ', reverse(@str), charindex(' ', reverse(@str)) + 1) - 1)

将此功能合并到现有查询中,以下内容使用CTE

; with cte as
(
  -- your existing query
  SELECT    
          STUFF(  (  
                    SELECT  ' , Via ' 
                            + CAST(ROW_NUMBER() OVER 
                                   (ORDER BY  t1.BookingId asc) AS VARCHAR(4)) 
                            + ' : ' +  t2.ViaLocValue    
                    FROM  Booking_ViaLocations t2  
                    WHERE t2.BookingId = t1.BookingId  
                    FOR XML PATH ('')
                   )  
                  ,1,2,'') AS Names  
  FROM   Booking_ViaLocations t1  
  WHERE  t1.BookingId = b.Id  
  GROUP BY t1.BookingId
)
select *,
       right(Names, 
             charindex(' ', reverse(Names), 
             charindex(' ', reverse(Names)) + 1) - 1) as PostCode
from   cte

查询3:

  SELECT    
          STUFF(  (  
                    SELECT  ' , Via ' 
                            + CAST(ROW_NUMBER() OVER 
                                   (ORDER BY  t1.BookingId asc) AS VARCHAR(4)) 
                            + ' : ' +  
             right('  ' + t2.ViaLocValue, 
             charindex(' ', reverse('  ' + t2.ViaLocValue), 
             charindex(' ', reverse('  ' + t2.ViaLocValue)) + 1) - 1)
                    FROM  Booking_ViaLocations t2  
                    WHERE t2.BookingId = t1.BookingId  
                    FOR XML PATH ('')
                   )  
                  ,1,2,'') AS Names  
  FROM   Booking_ViaLocations t1  
  WHERE  t1.BookingId = b.Id  
  GROUP BY t1.BookingId

答案 1 :(得分:1)

current_line