Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



T-SQL: Join Tables by a Field that contains a delimited string

This is the first time I ran into a situation where I needed to Join two tables (one with a varchar field and one with a varchar field that contains pipe delimited data) so I decided to post it for other who may not know you can do this. Below is a simplified example with solution of getting all rows of the first table whos values are contained in the pipe delimited field of the second table. This is really simple to do and you don't even have to use any User-Defined Functions or Cursors to do it. And I'm sure that not all of you who may read this know that you can use LIKE in a JOIN.

1 SELECT City
2 FROM Table1
3 JOIN Table2 ON Table2.CityList LIKE '%' + Table1.City + '%'


Table1
City varchar(30)

Table1 Sample Data
West Bend
Kewaskum
Hartford

Table2
CityList varchar(2000)

Table2 Sample Data
West Bend|Kewaskum
Kewaskum
Hartford

 

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: General
Posted by crpietschmann on Friday, December 09, 2005 6:16 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann