Complicated select and update

I have a totally messed up table I've received that has a column called "Vendor"
The Vendor can be a company name, a PO Box or an actual name

The actual names ALL come in as
First Last
or
First M Last
First M. Last

What I want to do is use the columns where there are 1 or 2 single spaces
left join them to my table variable below with the list of first names as an added "this is a name" filter

and parse out the first, middle, last
DECLARE @firstNames TABLE 
(
  firstName varchar(100)
)

Insert into @firstNames (firstName) 
Select tmp.firstName  from (

Select firstName from portal.dbo.r_users
union all
Select firstName from proc_ss.dbo.P_Deals
union all
select firstName from proc_lt.dbo.P_Deals
union all
select ordFirstName firstName from proc_cfa.dbo.P_Order
union all
select first_name from crmprod_01.dbo.leads
union all
select first_name from crmlotto_prod.dbo.leads) tmp
group by tmp.firstName
order by tmp.firstName

Select * from @firstNames

Open in new window

Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LowfatspreadCommented:
sorry dont follow what you're after...

could you try specifying what you want again , with some example Input and Output...

(ps... there is no point in using UNION ALL if you are going to distinct with a group by .... just use UNION in the first place
   and don't have the group by... the order by is fairly meaningless as well.)
0
Scott PletcherSenior DBACommented:

DECLARE @firstNames TABLE 
(
  firstName varchar(100) primary key
)

Insert into @firstNames (firstName) 
Select tmp.firstName  from (
Select firstName from portal.dbo.r_users
union
Select firstName from proc_ss.dbo.P_Deals
union
select firstName from proc_lt.dbo.P_Deals
union
select ordFirstName firstName from proc_cfa.dbo.P_Order
union
select first_name from crmprod_01.dbo.leads
union
select first_name from crmlotto_prod.dbo.leads) tmp
order by tmp.firstName

Select * from @firstNames


SELECT tn.*, CASE WHEN fn.firstName IS NULL THEN 'Not Name' ELSE 'Name' END
FROM tablename tn
LEFT OUTER JOIN @firstNames fn ON
    fn.firstName = LEFT(tn.Vendor, CHARINDEX(SPACE(1), tn.Vendor) - 1)
WHERE
    tn.Vendor LIKE '% %' OR
    tn.Vendor LIKE '% % %'

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
Change the LEFT OUTER to an INNER join if you want to select only matching first names.
0
Larry Bristersr. DeveloperAuthor Commented:
perfect
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.