T-SQL Case Statement in a JOIN ON Clause

后端 未结 6 1998
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 15:09

I am trying to build a case/if statement in a JOIN ON clause.

LEFT JOIN [CTSTRC] [Statuses] ON RIGHT([Statuses].[STRID], 3) = [CTE].[F61]
         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 15:23

    Wouldn't something like this work:

    LEFT JOIN [CTSTRC] [Statuses] ON RIGHT([Statuses].[STRID], 3) = cast([CTE].[F61] as varchar(3))
    

    All you really care about is whether you have a match, so why not convert the numeric to varchar? You would have to test both ideas to see which one is faster.

    I do agree with @KM, fixing a bad design like this is the best solution. Having functions and Case statements in a join is an indicatior that your design is fatally flawed and should be fixed.

提交回复
热议问题