Unix Technical Forum

SQL Using LIKE Command with Subquery

This is a discussion on SQL Using LIKE Command with Subquery within the SQL Server forums, part of the Microsoft SQL Server category; --> I am try to update the Gender field for all females of a database with about 15,000 records. So ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 06:48 AM
coryjflynn@hotmail.com
 
Posts: n/a
Default SQL Using LIKE Command with Subquery

I am try to update the Gender field for all females of a database with
about 15,000 records. So how I started was by searching baby girl
names on the web and manipulated some of there lists to create my own
table of female first names (HSNames). I can then use this table to
update the Gender field to 'F' (female) in the master table. This
process works great using the following query...

Update HSContacts
Set Gender = 'F'
Where First_Name IN
(Select FirstName
>From HSNames

Where Sex = 'F')

HSContacts is the master table with fields of Contact_UNO, First_Name,
Last_Name, Full_Name, Gender, etc..). HSNames contains fields named
UniqueID, FirstName, Sex.

Here is where the problem comes in... When we imported the field data
into the database many of the first name fields in the master table
also contained the middle initial (ex. Mary A.). How can I change my
query to include these records? Can I take use of the SQL LIKE command
with a subquery? Every way I look at it I get an error in SQL Server
2000 saying I cannot have a subquery spitting out multiple values with
this expression.

Any help would be appreciated. Thx.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 06:48 AM
Erland Sommarskog
 
Posts: n/a
Default Re: SQL Using LIKE Command with Subquery

(coryjflynn@hotmail.com) writes:
> Update HSContacts
> Set Gender = 'F'
> Where First_Name IN
> (Select FirstName
> From HSNames
> Where Sex = 'F')
>
> HSContacts is the master table with fields of Contact_UNO, First_Name,
> Last_Name, Full_Name, Gender, etc..). HSNames contains fields named
> UniqueID, FirstName, Sex.
>
> Here is where the problem comes in... When we imported the field data
> into the database many of the first name fields in the master table
> also contained the middle initial (ex. Mary A.). How can I change my
> query to include these records? Can I take use of the SQL LIKE command
> with a subquery?


Yes, but you neeed to switch to EXISTS:

Update HSContacts
Set Gender = 'F'
FROM HSConcatcs c
Where EXISTS (Select *
From HSNames n
Where n.Sex = 'F'
AND c.FirstName LIKE n.FirstName + '%')



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 06:48 AM
coryjflynn@hotmail.com
 
Posts: n/a
Default Re: SQL Using LIKE Command with Subquery

Of-course! I feel like an idiot. I can't believe I couldn't come up
with that. Thanks for getting me through my brain lapse...


Erland Sommarskog wrote:
> (coryjflynn@hotmail.com) writes:
> > Update HSContacts
> > Set Gender = 'F'
> > Where First_Name IN
> > (Select FirstName
> > From HSNames
> > Where Sex = 'F')
> >
> > HSContacts is the master table with fields of Contact_UNO,

First_Name,
> > Last_Name, Full_Name, Gender, etc..). HSNames contains fields

named
> > UniqueID, FirstName, Sex.
> >
> > Here is where the problem comes in... When we imported the field

data
> > into the database many of the first name fields in the master table
> > also contained the middle initial (ex. Mary A.). How can I change

my
> > query to include these records? Can I take use of the SQL LIKE

command
> > with a subquery?

>
> Yes, but you neeed to switch to EXISTS:
>
> Update HSContacts
> Set Gender = 'F'
> FROM HSConcatcs c
> Where EXISTS (Select *
> From HSNames n
> Where n.Sex = 'F'
> AND c.FirstName LIKE n.FirstName + '%')
>
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techinf...2000/books.asp


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:52 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com