|
Hi, I am trying to find a solution for the below problem, but could not think of any way. The problem is, i need to find if a givenword exist in a table. if 1-- then Asian country if 2- north american country if 3-- rest of the world given list of words 'India,Srilanka,China'. The question is: get country names with value "1" and is in the given list of words. If the countryshows up with value 1 but not in the list of words, then code need to be changed to 3. Expected output : india 1 indian islands 1 srilanka 1 islands of srilanka 1 china 1 if select * from @country then expected result is india 1, indian islands 1, srilanka 1, islands of srilanka 1, china 1, us 2, canada 2, uk 3, france 3. plzz help me out how to accomplish this. thanks in adv
(comments are locked)
|
|
Here is your data repeated here: Can I get an A? :) Oleg <!-- Begin Edit Per @roshan request, lets change the requirement to have the number of items in the list of words dynamic. This way, we don't have to have a complex and possibly long where clause: After the original table is created and populated with data, declare table to keep one word per record: The above update will update 2 records like before. End Edit --> I was expecting something which could be applied to any no(in given words). what if i have 50 given words then i cant put %@word% 50times in my where clause.
Jan 19 '11 at 12:36 PM
roshan
@roshan I have updated my answer so the list can be dynamic. Please let me know if this works. By the way, since you have SQL Server 2008, the best way to make it work is to create a stored procedure which accepts a given code and also a table valued paramater populated with the list of desired words. There was a question about 6 months ago asking about how to update the database table with values from C# session. My answer included a complete sample how to do it, which you might find useful if you opt to go this route.
Jan 19 '11 at 01:08 PM
Oleg
@Oleg,Thanks for the answer. In the mean time i have written a Table valued function which converts CSV To CharTable. This function accepts the given words('India,srilanka,china') as input and displays them as a records in the table. Can this be used in anyway to reduce the code?? instead of writing 50 select union all.
Jan 19 '11 at 01:44 PM
roshan
@roshan Yes, but there is no need to write your own TVF to convert delimited data. You can use the one written by Jeff Moden, it is the best one. Please take a look at this discussion (if you already have main SSC account), it has Jeff's function, my own which uses xml and CLR function by Pavel Pawlowski.
Jan 19 '11 at 02:15 PM
Oleg
(comments are locked)
|
|
EDIT Updated the SELECT-statement after Oleg pointed out I hadn't read the question good enough. END EDIT Have a look at Jeff Modens article about Tally tables: **http://www.sqlservercentral.com/articles/T-SQL/62867/** Here's an example based on Jeffs article, which I think will solve your problem: @Magnus Ahlkvist This specific predicate will not work, what is needed is not the IN (some_list) but the LIKE predicate. I believe that my answer (after the begin edit part) does the trick. In other words, you should probably consider modifying your select to select
c.countryname, c.code
from @country c inner join
(
select SplitWord
from dbo.fn_Split('%India%,%Srilanka%,%China%')
) words
on c.code = 1
and c.countryname like words.SplitWord;or restate the above as cross apply :) Otherwise, you will not get back the result for, say, 'islands of srilanka' record. Same goes for the update @roshan requested: update c
set code = 3
from @country c left join
(
select SplitWord
from dbo.fn_Split('%India%,%Srilanka%,%China%')
) words
on c.countryname like words.SplitWord
where c.code = 1 and words.SplitWord is null;
Jan 19 '11 at 04:07 PM
Oleg
Oh, you're right. I over-simplified thinkgs. I'm guessing that the input string comes from outside and cannot easily be changed to '%India%,%Srilanka%,%China%' Instead I would do
Jan 19 '11 at 04:15 PM
Magnus Ahlkvist
Yes, this is a very good catch, I definitely missed this one. select '%' + SplitWord + '%' as word is much better than what I had in my comment, sandwiching each word between the percent signs :)
Jan 19 '11 at 04:26 PM
Oleg
(comments are locked)
|


Sorry but it smells homework!?
i have just give a test data. i would have written the same ans as Oleg submitted if it was a homework. But i want something more.