SQL Essentials Training & Certification
- 7k Enrolled Learners
- Self Paced
SQL Server allows you to execute real-time programmatic logic on the values within your query. Based on these logical evaluations, you can generate values as part of the returned data set. In this blog, you will learn how to implement if statement in SQL with examples. Below are the topics covered in this blog –
IF() function is passed with two parameters, one for true and other for false. The function returns one value if a condition is TRUE, and another value if the condition is FALSE.
IF(condition, value_if_true, value_if_false)
Required. The value to test
Optional. The value to return if condition is TRUE
Optional. The value to return if condition is FALSE
Return 0 if the condition is TRUE, or 1 if the condition is FALSE:
SELECT IF(100<500, 0, 1);
SELECT IF(900<500, 0, 1);
Moving ahead with IF Statement in SQL, let’s see some String examples.
Test If condition using Strings
If the two strings are same, the query returns “YES” else it returns “NO”
SELECT IF(STRCMP(“hello”,“learner”) = 0, “YES”, “NO”);
SELECT IF(STRCMP(“hello”,“hello”) = 0, “YES”, “NO”);
With this, we come to an end of this blog on “If statement in SQL”. I hope it added to your knowledge. If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.