if else in select Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DanielT
    New Member
    • Jan 2009
    • 4

    if else in select Statement

    Hi guys, I am new here. I need to put the below if else condition in select statement, is it possible?

    if task_is_trunk = 1
    (select branch_name from dbo.tblBranch where task_location_g uid = dbo.tblBranch.b ranch_guid) as location_name
    else
    (select location_id from dbo.tblCSLocati on where location_guid = dbo.tblCSTask.t ask_location_gu id) as location_name

    Thanks!
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Your query might return multiple result sets. What are you trying to do with this code?

    -- CK

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      I think this would work "correctly" .
      (select branch_name from dbo.tblBranch where task_location_g uid = dbo.tblBranch.b ranch_guid AND task_is_trunk = 1) as location_name
      Union
      (select location_id from dbo.tblCSLocati on where location_guid = dbo.tblCSTask.t ask_location_gu id AND task_is_trunk <> 1) as location_name

      Comment

      • DanielT
        New Member
        • Jan 2009
        • 4

        #4
        thx for ur reply guys, I solved the problem by joining all those tables, and query as below

        Code:
        SELECT 	'location_name' = CASE
        					WHEN t.task_is_trunk = 1 THEN
        						dbo.tblbranch.branch_name
        					ELSE
        						dbo.tblCSLocation.location_id
        					END

        Comment

        Working...