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.
| Author |
Topic |
|
fuzzyip
Starting Member
35 Posts |
Posted - 2008-07-30 : 22:05:26
|
| I have a column that has the name of hotels in it called Hotelname. I want to create a calculated field next to it called Hotelchain that states the company that owns that hotelFor example if I had the following in the Hotelname columnHotelnameResidence InnJ W Las Vegas ResortLas Vegas Marriott SuitesThe London --owned by MarriottCourtyard by Marriott Las Vegas Convention CenterSuper 8 - Las Vegas Strip AreaI want the Hotelchain column to stateHotelChainResidence inn LimitedJ Wynn ResortsMarriottMarriottMarriottSuper 8 CompanyMy idea is to do a case statement with the list of every hotelname and its corresponding hotelchain. For example I would do something like, if hotelname like "marriott" then hotelchain = "marriott", else if hotelname like "the london" then hotelchain = "marriott", else if hotelname like "super 8" then hotelchain = "super 8 company", else if hotelname like "residence inn" then hotelchain = "residence inn limited", else if hotelname like "j w" then hotelchain = "J Wynn Resorts"I would do the above by doing thisSELECT Hotelname, Hotelchain = CASE hotelname WHEN hotelname = "marriott" or "The London" THEN 'Marriott' WHEN hotelname = "Super 8" THEN 'Super 8 Company' ELSE 'Unknown' END,FROM HotelsWould this work? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-30 : 22:18:40
|
This should work
SELECT Hotelname, Hotelchain = CASE hotelname WHEN hotelname LIKE "%marriott%" or "The London" THEN 'Marriott' WHEN hotelname LIKE "%Super 8%" THEN 'Super 8 Company' ELSE 'Unknown' ENDFROM Hotels KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
fuzzyip
Starting Member
35 Posts |
Posted - 2008-07-31 : 11:49:09
|
quote: Originally posted by khtan This should work
SELECT Hotelname, Hotelchain = CASE hotelname WHEN hotelname LIKE "%marriott%" or "The London" THEN 'Marriott' WHEN hotelname LIKE "%Super 8%" THEN 'Super 8 Company' ELSE 'Unknown' ENDFROM Hotels KH[spoiler]Time is always against us[/spoiler]
just ran this, got a Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'LIKE'.it looks correct to me too, anyone see the error? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-31 : 12:51:07
|
| [code]SELECT Hotelname, Hotelchain = CASE hotelname WHEN hotelname LIKE '%marriott%' or hotelname LIKE 'The London' THEN 'Marriott' WHEN hotelname LIKE '%Super 8%' THEN 'Super 8 Company' ELSE 'Unknown' ENDFROM Hotels[/code]use single quotes |
 |
|
|
fuzzyip
Starting Member
35 Posts |
Posted - 2008-07-31 : 13:00:37
|
quote: Originally posted by visakh16
SELECT Hotelname, Hotelchain = CASE hotelname WHEN hotelname LIKE '%marriott%' or hotelname LIKE 'The London' THEN 'Marriott' WHEN hotelname LIKE '%Super 8%' THEN 'Super 8 Company' ELSE 'Unknown' ENDFROM Hotels use single quotes
Got it, it was a combination of the single quotes and the "hotelname" right after the case that got it, should be case when, not case hotelname whenThanks guys |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-31 : 13:04:44
|
quote: Originally posted by fuzzyip
quote: Originally posted by visakh16
SELECT Hotelname, Hotelchain = CASE hotelname WHEN hotelname LIKE '%marriott%' or hotelname LIKE 'The London' THEN 'Marriott' WHEN hotelname LIKE '%Super 8%' THEN 'Super 8 Company' ELSE 'Unknown' ENDFROM Hotels use single quotes
Got it, it was a combination of the single quotes and the "hotelname" right after the case that got it, should be case when, not case hotelname whenThanks guys
just noticed, you need to remove column name near CASE |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-01 : 06:06:01
|
quote: Originally posted by visakh16 use single quotes
My eyesight is failling KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|