Unix Technical Forum

Need SELECT ... LIKE help, please

This is a discussion on Need SELECT ... LIKE help, please within the SQL Server forums, part of the Microsoft SQL Server category; --> I've got the following SELECT statement in a stored procedure: SELECT FirstName, LastName, BusinessName, MailingAddress1 FROM tblAddresses WHERE FirstName ...


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 03-01-2008, 03:26 PM
manning_news@hotmail.com
 
Posts: n/a
Default Need SELECT ... LIKE help, please

I've got the following SELECT statement in a stored procedure:

SELECT FirstName, LastName, BusinessName, MailingAddress1
FROM tblAddresses
WHERE FirstName LIKE '%' + ISNULL(@FirstName, '') + '%' AND
LastName LIKE '%' + ISNULL(@LastName, '') + '%'

If I input "tom" for @FirstName then it returns rows with "tom"
somewhere in the first name but that row must have a last name too in
order to be returned. How do I change this to return both types of
rows: one with "tom" as a firstname and with a lastname and one with
"tom" as first name but no last name?

Thanks for any help or advice.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:26 PM
Jason Lepack
 
Posts: n/a
Default Re: Need SELECT ... LIKE help, please

SELECT
FirstName,
LastName,
BusinessName,
MailingAddress1
FROM
tblAddresses
WHERE
FirstName LIKE '%' + ISNULL(@FirstName, '') + '%'
AND
(
LastName LIKE '%' + ISNULL(@LastName, '') + '%'
OR LastName is null
)





On Jul 5, 4:56 pm, manning_n...@hotmail.com wrote:
> I've got the following SELECT statement in a stored procedure:
>
> SELECT FirstName, LastName, BusinessName, MailingAddress1
> FROM tblAddresses
> WHERE FirstName LIKE '%' + ISNULL(@FirstName, '') + '%' AND
> LastName LIKE '%' + ISNULL(@LastName, '') + '%'
>
> If I input "tom" for @FirstName then it returns rows with "tom"
> somewhere in the first name but that row must have a last name too in
> order to be returned. How do I change this to return both types of
> rows: one with "tom" as a firstname and with a lastname and one with
> "tom" as first name but no last name?
>
> Thanks for any help or advice.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:26 PM
manning_news@hotmail.com
 
Posts: n/a
Default Re: Need SELECT ... LIKE help, please

On Jul 5, 4:01 pm, Jason Lepack <jlep...@gmail.com> wrote:
> SELECT
> FirstName,
> LastName,
> BusinessName,
> MailingAddress1
> FROM
> tblAddresses
> WHERE
> FirstName LIKE '%' + ISNULL(@FirstName, '') + '%'
> AND
> (
> LastName LIKE '%' + ISNULL(@LastName, '') + '%'
> OR LastName is null
> )
>
> On Jul 5, 4:56 pm, manning_n...@hotmail.com wrote:
>
>
>
> > I've got the following SELECT statement in a stored procedure:

>
> > SELECT FirstName, LastName, BusinessName, MailingAddress1
> > FROM tblAddresses
> > WHERE FirstName LIKE '%' + ISNULL(@FirstName, '') + '%' AND
> > LastName LIKE '%' + ISNULL(@LastName, '') + '%'

>
> > If I input "tom" for @FirstName then it returns rows with "tom"
> > somewhere in the first name but that row must have a last name too in
> > order to be returned. How do I change this to return both types of
> > rows: one with "tom" as a firstname and with a lastname and one with
> > "tom" as first name but no last name?

>
> > Thanks for any help or advice.- Hide quoted text -

>
> - Show quoted text -


OK, thank you! I was missing the "OR LastName is null)" The proc
works great now.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 03:26 PM
Roy Harvey
 
Posts: n/a
Default Re: Need SELECT ... LIKE help, please

I believe that will return Tom NULL when they ask for Tom Smith. If
they intend to reject a NULL last name when a last name is give then
the WHERE clause would have to change slightly.

WHERE
FirstName LIKE '%' + ISNULL(@FirstName, '') + '%'
AND
(
LastName LIKE '%' + ISNULL(@LastName, '') + '%'
OR (LastName is null AND @LastName IS NULL)
)

Roy Harvey
Beacon Falls, CT

On Thu, 05 Jul 2007 14:01:15 -0700, Jason Lepack <jlepack@gmail.com>
wrote:

>SELECT
> FirstName,
> LastName,
> BusinessName,
> MailingAddress1
>FROM
> tblAddresses
>WHERE
> FirstName LIKE '%' + ISNULL(@FirstName, '') + '%'
> AND
> (
> LastName LIKE '%' + ISNULL(@LastName, '') + '%'
> OR LastName is null
> )
>
>
>
>
>
>On Jul 5, 4:56 pm, manning_n...@hotmail.com wrote:
>> I've got the following SELECT statement in a stored procedure:
>>
>> SELECT FirstName, LastName, BusinessName, MailingAddress1
>> FROM tblAddresses
>> WHERE FirstName LIKE '%' + ISNULL(@FirstName, '') + '%' AND
>> LastName LIKE '%' + ISNULL(@LastName, '') + '%'
>>
>> If I input "tom" for @FirstName then it returns rows with "tom"
>> somewhere in the first name but that row must have a last name too in
>> order to be returned. How do I change this to return both types of
>> rows: one with "tom" as a firstname and with a lastname and one with
>> "tom" as first name but no last name?
>>
>> Thanks for any help or advice.

>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 03:26 PM
manning_news@hotmail.com
 
Posts: n/a
Default Re: Need SELECT ... LIKE help, please

On Jul 5, 4:32 pm, Roy Harvey <roy_har...@snet.net> wrote:
> I believe that will return Tom NULL when they ask for Tom Smith. If
> they intend to reject a NULL last name when a last name is give then
> the WHERE clause would have to change slightly.
>
> WHERE
> FirstName LIKE '%' + ISNULL(@FirstName, '') + '%'
> AND
> (
> LastName LIKE '%' + ISNULL(@LastName, '') + '%'
> OR (LastName is null AND @LastName IS NULL)
> )
>
> Roy Harvey
> Beacon Falls, CT
>
> On Thu, 05 Jul 2007 14:01:15 -0700, Jason Lepack <jlep...@gmail.com>
> wrote:
>
>
>
> >SELECT
> > FirstName,
> > LastName,
> > BusinessName,
> > MailingAddress1
> >FROM
> > tblAddresses
> >WHERE
> > FirstName LIKE '%' + ISNULL(@FirstName, '') + '%'
> > AND
> > (
> > LastName LIKE '%' + ISNULL(@LastName, '') + '%'
> > OR LastName is null
> > )

>
> >On Jul 5, 4:56 pm, manning_n...@hotmail.com wrote:
> >> I've got the following SELECT statement in a stored procedure:

>
> >> SELECT FirstName, LastName, BusinessName, MailingAddress1
> >> FROM tblAddresses
> >> WHERE FirstName LIKE '%' + ISNULL(@FirstName, '') + '%' AND
> >> LastName LIKE '%' + ISNULL(@LastName, '') + '%'

>
> >> If I input "tom" for @FirstName then it returns rows with "tom"
> >> somewhere in the first name but that row must have a last name too in
> >> order to be returned. How do I change this to return both types of
> >> rows: one with "tom" as a firstname and with a lastname and one with
> >> "tom" as first name but no last name?

>
> >> Thanks for any help or advice.- Hide quoted text -

>
> - Show quoted text -


Yes, you're right. Thanks for the tip.

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 01:14 PM.


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