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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SELECT CASE, need 'like' behavior

Author  Topic 

sql777
Constraint Violating Yak Guru

314 Posts

Posted - 2008-07-12 : 15:43:13
Hi,
I need to do something like:

SELECT category =
CASE url
WHEN like 'www.example.com/articles/%' THEN 'articles'
WHEN like 'www.example.com/forums/%' THEN 'forums'
ELSE ''
END
FROM content

How can I achieve this?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-12 : 15:45:27
Try this:


SELECT category =
CASE
WHEN url LIKE '...%' THEN 'articles'
WHEN url LIKE '...%' THEN 'forums'
ELSE ''
END
FROM content


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sql777
Constraint Violating Yak Guru

314 Posts

Posted - 2008-07-12 : 17:22:23
Thanks!

I was doing:

select category =
case URL
WHEN url like. ...

case URL was causing the error!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-07-14 : 03:28:45
quote:
Originally posted by sql777

Thanks!

I was doing:

select category =
case URL
WHEN url like. ...

case URL was causing the error!


Note that the CASE expression you used is simple CASE which can be used to test for equality only. Otherwise you need to use searched CASE which can be used for any comparison

Thats why I always suggest to use

CASE when expression...

than

CASE expression when

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-14 : 03:48:26
Which is very well explained in Books Online.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -