如何在SQL中合并两列

时间:2013-08-02 04:47:07

标签: sql sql-server

这是我的Qyery

 Select FinancialId,
        ApplicationId,
        UPPER (InterestSubsidyFinancialDetail.BankName) as BankName,    
        InterestSubsidyFinancialDetail.BankId,
        BankMaster.BankName as Bank

 From   InterestSubsidyFinancialDetail

 left join BankMaster On BankMaster.BankId = InterestSubsidyFinancialDetail.BankId

这个查询给了我这样的结果:

FinancialId   ApplicationId     BankName       BankId   Bank
1              1                HARSH BANK        -1    NULL
7              3                                   1    Allahabad Bank
8              3                JFGJFGJ           -1    NULL

但我需要将“BankName”和“Bank”列合并为:

FinancialId   ApplicationId     BankName       BankId   
1              1                HARSH BANK        -1    
7              3                Allahabad Bank     1                
8              3                JFGJFGJ           -1    

这有可能得到我需要的结果吗?如果是的请帮助我 感谢

5 个答案:

答案 0 :(得分:4)

你可以使用它。

Select FinancialId,
        ApplicationId,
        ISNULL(UPPER (InterestSubsidyFinancialDetail.BankName),'') + 
        ISNULL(BankMaster.BankName,'')
        as BankName,    
        InterestSubsidyFinancialDetail.BankId

 From   InterestSubsidyFinancialDetail

 left join BankMaster On BankMaster.BankId = InterestSubsidyFinancialDetail.BankId

答案 1 :(得分:0)

请尝试:

select 
    FinancialId,
    ApplicationId,
    ISNULL(UPPER (InterestSubsidyFinancialDetail.BankName), '')+ISNULL(BankMaster.BankName, '') as BankName,    
    InterestSubsidyFinancialDetail.BankId
From   
    InterestSubsidyFinancialDetail left join 
        BankMaster On BankMaster.BankId = InterestSubsidyFinancialDetail.BankId

答案 2 :(得分:0)

SELECT CONCAT(BankName, Bank)
FROM InterestSubsidyFinancialDetail

答案 3 :(得分:0)

Select FinancialId,
    ApplicationId,
    UPPER (ISNULL(InterestSubsidyFinancialDetail.BankName,''))+''+ISNULL(BankMaster.BankName,'') as BankName,    
    InterestSubsidyFinancialDetail.BankId

From   InterestSubsidyFinancialDetail

left join BankMaster On BankMaster.BankId = InterestSubsidyFinancialDetail.BankId

答案 4 :(得分:-1)

使用COALESCE功能,这正是您所需要的:

SELECT FinancialId,
       ApplicationId,
       COALESCE(UPPER(InterestSubsidyFinancialDetail.BankName), BankMaster.BankName) as BankName,    
       InterestSubsidyFinancialDetail.BankId
FROM   InterestSubsidyFinancialDetail
LEFT JOIN BankMaster 
     ON BankMaster.BankId = InterestSubsidyFinancialDetail.BankId