sql server比较两个字段 [英] sql server compare two field

查看:236
本文介绍了sql server比较两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在内部联接中执行通配符搜索时遇到问题.

我有两个表,如下所示

第一个是

I am facing problem while performing wildcard search in inner join.

I have two tables as follows

first one is

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

StateCode
 02
 10
 11
 12
 13
 14
 16



我想编写一个查询,将 StateCode Code 进行比较,并用相应的Name替换StateCode并给我输出像这样




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


SatateCode    Name 
 10          Haryana
 02          Andhra Pradesh



等等……





and so on......



Any alternative approach would be great helpful.

推荐答案

如果第一个是T1&第二个是T2,那么此查询将是:

从T1内部联接T2中选择T2.statecode作为SatateCode,T1.Name,其中T2.StateCode == CONVERT(INT,Replace(T1.StateCode,''IN.'',``''))

我认为这是您的解决方案!
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 !


UPDATE T1.Name = T2.StateCode
FROM Table1 T1
INNER JOIN Table2 T2 ON T2.StateCode==REPLACE(T1.StateCode,'IN.','')



-试试这个,对你有帮助.



-- Try This, Its helps u.


这篇关于sql server比较两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆