1

I have a SQL Server tables with states in them and another table with states in them but multiple states in a cell:

StateTable1

           Col 1             Col 2            
Row1      FullName          WA, OH, FL

GeographyTable2

            Col 1            Col 2            
Row1      Washington           WA
Row2        Ohio               OH
Row3       Florida             FL

FinalResultingTable3

           Col 1              Col 2            
 Row1     FullName             WA
 Row2     FullName             OH
 Row3     FullName             FL  

What I want to do is join the Geographycol2 table to Statecol2 table and return the final table where I basically return multiple rows for each state.

Maybe the join isn't the place to do this is there a way to transpose these csv into rows with the same fullname?

Thanks for the help

Attempted query (where Contact is State table):

Select Top 1000
    Ct.fullname, Ct.ActiveLicenses, Geo.state
From 
    datawarehouse.wby_vw_dim.contact Ct
join 
    datawarehouse.wby_vw_dim.geography Geo on Geo.state like '%' + Ct.activelicenses + '%'

and this just returns one fullname infinite times

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Srboarder91
  • 11
  • 2
  • 7

4 Answers4

0

change the join to

on Ct.activelicenses like '%' + Geo.state + '%'

instead of

on Geo.state like '%' + Ct.activelicenses + '%'
Hedinn
  • 864
  • 4
  • 7
0

Playing with your example...

select 
  GeographyTable2.column1,
  StateTable1.column1,
  GeographyTable2.column2
from
  GeographyTable2 left join StateTable1 on 
    StateTable1.column2 like '%'+GeographyTable2.column2+'%';

You can find a SQL Fiddle demo here.

David Isla
  • 619
  • 7
  • 19
0

Your best bet it is to put the data into a related table. Since you say you can't fix the schema, then you need to create a temp table and split the data out. Then join to the temp table as you would if you have this data correctly designed in the first place. Here is an example of how you would create the data for them temp table:

CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END


select * from dbo.fnSplitString('q,d,b', ',') split
create table #test (Id int, mytest varchar (10))

insert into #test
values (1, 'a,b')
, (2, 'c,d,e')
, (3, 'f,g')
, (4, 'h')

create table #Splitdata (ID int, splitdata varchar (10))

insert into #Splitdata (id, Splitdata)
select t.id, split.splitdata from #test t
cross apply dbo.fnSplitString(mytest, ',') split

Note that I borrowed the split function from here: http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/

Once you have the data into #splitdata table, you then join to it like normal using it a s a junction table between you two original table.s

HLGEM
  • 94,695
  • 15
  • 113
  • 186
0

I use a recursive cte to split the string and remove extra spaces. Then join to the other table.

Sql Fiddle Demo

;with tmp([Col 1], DataItem, [Col 2]) as (  
    select 
        [Col 1], 
        LEFT([Col 2], CHARINDEX( ',', [Col 2]+',')-1), 
        ltrim(rtrim(STUFF([Col 2], 1, CHARINDEX(',',[Col 2]+','), '')))
    from StateTable1
    union all
    select 
        [Col 1], 
        LEFT([Col 2], CHARINDEX(',',[Col 2]+',')-1),
        ltrim(rtrim(STUFF([Col 2], 1, CHARINDEX(',',[Col 2]+','), '')))
    from tmp
    where [Col 2] > ''
)
SELECT t.[Col 1], DataItem, g.[Col 1]
FROM tmp t 
inner join GeographyTable2 g 
on t.DataItem = g.[Col 2]

OUTPUT

|    Col 1 | DataItem |      Col 1 |
|----------|----------|------------|
| FullName |       WA | Washington |
| FullName |       OH |       Ohio |
| FullName |       FL |    Florida |

I use this example

Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118