Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am facing problem while performing wildcard search in inner join.

I have two tables as follows

first one is

SQL
Code	Name
IN.01	Andaman 
IN.02	Andhra Pradesh
IN.03	Assam
IN.05	Chandīgarh
IN.06	Dādra 
IN.07	NCT
IN.09	Gujarāt
IN.10	Haryana


second one is

SQL
StateCode
 02
 10
 11
 12
 13
 14
 16


I want to write a query which will compare StateCode with Code and replace StateCode with corresponding Name and give me output like this


SQL
SatateCode    Name 
 10          Haryana
 02          Andhra Pradesh


and so on......



Any alternative approach would be great helpful.
Posted

If first one is T1 & second one is T2 then this query will be:

SELECT T2.statecode as SatateCode,T1.Name from T1 innerjoin T2 where T2.StateCode== CONVERT(INT, Replace(T1.StateCode,'IN.','')

I think its your solution !
 
Share this answer
 
Comments
vikram_shinde 14-Mar-12 7:12am    
StateCode are nvarchar . So still I have to use CONVERT?
uttam2010 14-Mar-12 7:25am    
then its so simple like this:
SELECT T2.statecode as SatateCode,T1.Name from T1 inner join T2 where T2.StateCode== Replace(T1.StateCode,'IN.','')
vikram_shinde 14-Mar-12 7:30am    
no luck .... it's giving error... says Incorrect syntax near the keyword 'where'
uttam2010 14-Mar-12 7:41am    
SELECT T2.statecode as SatateCode,T1.Name from T1 inner join T2 on T2.StateCode== Replace(T1.StateCode,'IN.','')

Sorry..i missed 'ON'
good luck
SQL
UPDATE T1.Name = T2.StateCode
FROM Table1 T1
INNER JOIN Table2 T2 ON T2.StateCode==REPLACE(T1.StateCode,'IN.','')


-- Try This, Its helps u.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900