Check for if else in SQL query

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 839 Views - Last Post: 12 April 2017 - 10:08 PM

#16 fearless2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 29-February 16

Re: Check for if else in SQL query

Posted 12 April 2017 - 10:08 PM

View Postmodi123_1, on 12 April 2017 - 07:05 PM, said:

What have you tried, attempted, or considered in the last three weeks?


First: I tried the following doesn't display record from table 1 if there is no matching of date in table 2
select t1.user_id,t1.date,t2.designation from table1 t1
left join table2 t2
   on tl.user_id=t2.user_id
where t2.from_date <= tl.date and (t2.thru_date >= tl.date or t2.thru_date is null)



Second: I have considered to use case method as below and tried, but I am not sure how to input the second criteria "xxx" or it is even correct because when I tried to replace "xxx" with 1, the record in table 1 will just duplicate.
select t1.user_id,t1.date,case when (t2.from_date <= tl.date and (t2.thru_date >= tl.date or t2.thru_date is null)) then t2.designation else "xxx" end from table1 t1
left join table2 t2
   on tl.user_id=t2.user_id



Trying to tweet around with the where clause.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2