September 17, 2010 at 2:54 am
I have been working on this since yesterday. could not find a way out.
I have two tables TableA and TableB
TableA
City
----
London
Ecblondontsr
sdclondon
londontres
thichicago
chicago
TableB
Input_city Output_City
---------- -------------
LONDON London
GREATER LONDON London
CITY OF LONDON London
CHICAGO Chicago
All I want is compare city column of TableA with Input_city column of TableB and set the city to output_city if they are equal.
Here equal means
ex: Ecnlondonstr contain LONDON in between, even though it should set to output_city
similarly sdclondon should also set to London as the city contains Input_city LONDON some where.
On a whole something like this
when city = %input_city
then set city = output_city.
when city= %input_city%
then set city = output_city
when city = input_city%
then set city = output_city.
But not sure how to use wildcard with column
Any help is highly appreciated.
September 17, 2010 at 3:03 am
SORRY Small change
On a whole something like this
when city LIKE %input_city
then set city = output_city.
when city LIKE %input_city%
then set city = output_city
when city LIKE input_city%
then set city = output_city.
September 17, 2010 at 3:26 am
have you tried by this :-
update table1 set city = a.inputcity
from table2 a
where ...... whatever condition you have
----------
Ashish
September 17, 2010 at 3:31 am
Thanks for the reply.
I have tied that,
but the thing is I dont have any idea of framing my condition, which need % with column name
September 17, 2010 at 3:35 am
if you can explain further that what exactly your table should be look like after running the query, i can try to write the query
----------
Ashish
September 17, 2010 at 3:36 am
DECLARE @TableA
TABLE (
city VARCHAR(50) NOT NULL
);
INSERT @TableA
(city)
VALUES ('London'),
('Ecblondontsr'),
('sdclondon'),
('londontres'),
('thichicago'),
('chicago');
DECLARE @TableB
TABLE (
input_city VARCHAR(50) NOT NULL,
output_city VARCHAR(50) NOT NULL
);
INSERT @TableB
(input_city, output_city)
VALUES ('LONDON', 'London'),
('GREATER LONDON', 'London'),
('CITY OF LONDON', 'London'),
('CHICAGO', 'Chicago');
UPDATE A
SET city =
(
SELECT B.input_city
FROM @TableB B
WHERE A.city LIKE '%' + B.input_city + '%'
)
FROM @TableA A
WHERE EXISTS
(
SELECT *
FROM @TableB B
WHERE A.city LIKE '%' + B.input_city + '%'
);
SELECT *
FROM @TableA;
September 17, 2010 at 3:38 am
My final table TableA shoulb be like this
TableA
City
-----
London
London
London
London
Chicago
Chicago
September 17, 2010 at 3:41 am
For your next question, try to provide sample data in the format I used, and be sure to include what you expect as output - it makes it easier for us, and gets you the answer you want faster 🙂
September 17, 2010 at 3:48 am
Sure Paul I will from next time.
I'll have a try on the solution u provide.
Thanks a lot.
September 17, 2010 at 3:56 am
No worries 🙂
If you are at all unsure how best to write a question, please take a look at this:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Paul
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply