956
Views
2
Comments
Solved
How do I add a if condition in SQL Query?
Question

Hi Community,

I am looking to add an if condition in the query. 

Something like this:

if {Notification).[SMSSeen]=1, then run the LEFT JOIN {UserSMSRespond} ON {UserSMSRespond}.[MobileNumber] = {Ticket}.[ContactNo] else don't need run but assign {UserSMSRespond} .[Message] = " "(empty)



2018-10-29 08-31-03
João Marques
 
MVP
Solution

Hi Alvin,


You can use the CASE command to control the field output.

So on your SELECT statement, instead of {UserSMSRespond}.[Message] you want to add CASE WHEN {Notification}.[SMSSeen] = 1 THEN {UserSMSRespond}.[Message] ELSE '' END


The syntax for the CASE is: CASE (condition) THEN (output) ELSE (output) END.

In this case you control which value your field will have.


Regarding the LEFT JOIN you will need to have it there to be able to use the {UserSMSRespond}.[Message] (in case {Notification}.[SMSSeen] = 1).


Hope it helps.


Cheers,

João

2026-02-26 06-29-24
Rahul
 
MVP

Hi Alvin,

i think you  need to try like this

if @SMSSeen=1

your sql statement

Else

without join sql statement


or you can see some case in sql join 

https://stackoverflow.com/questions/10256848/can-i-use-case-statement-in-a-join-condition 


Regards

Rahul

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.