Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Where Clause compare.

Author  Topic 

doerrs1
Starting Member

5 Posts

Posted - 2010-06-16 : 07:39:13
Hi All,

I have looked heavily trying to find a way to compare 2 columns in a Where Clause, where the 1st column's data has the left-most portion of the 2nd column's data.

The 'like' works for column to literal compares, but is there anything like a 'starts with' for comparing 2 columns?

Select * from .... (where column1 Like column2 for 4 char);

Are there any canned functions or would I have to write a function that parsed and compared only for a length?

Help! and thanks

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-06-16 : 07:52:58
is something like this what you're looking for? ->
DECLARE @names table (id int, firstname varchar(20), lastname varchar(20))

INSERT INTO @names
SELECT 1, 'henning', 'frettem' UNION ALL SELECT 2, 'henning', 'hennsomething'

SELECT * FROM @names WHERE lastname LIKE '%' + LEFT(firstname, 4) + '%'


- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page

doerrs1
Starting Member

5 Posts

Posted - 2010-06-16 : 08:47:37
quote:
Originally posted by Lumbago

is something like this what you're looking for? ->
DECLARE @names table (id int, firstname varchar(20), lastname varchar(20))

INSERT INTO @names
SELECT 1, 'henning', 'frettem' UNION ALL SELECT 2, 'henning', 'hennsomething'

SELECT * FROM @names WHERE lastname LIKE '%' + LEFT(firstname, 4) + '%'


- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com




Thank you Lumbago,
This works exactly like we need it to.
This forum is the best. I hope I can contribute at some point in time, but am the rookie for now.
thanks again,
Steve
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-06-16 : 09:05:04
Agreed! This forum is the best...I've built my entire career based on the stuff I've learned here

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -