Home Tutorials How to Use SQL CASE Statement

How to Use SQL CASE Statement

How to Use SQL CASE Statement

In SQL, the case statement gives the result based on criteria. Including the When, Order By, and Group By clauses, we may utilize a Case statement in select queries. It’s also possible to use it in the Insert statement.

CASE statement in SQL by Rajendra Gupta on April 1, 2019.


Use SQL CASE Statement

Pricing for ApexSQL

In SQL, the case statement returns a value based on a condition. Along with the Where, Order By, and Group By clauses, we may utilize a Case statement in select queries. It’s also possible to use it in the Insert statement. We’ll look at the CASE statement and its different applications in this post.

Assume you have a database that contains the ProductIDs for all of the items in a mini-store. You’re looking for the ProductName for a specific ProductID.

Consider the following scenario: We created a variable called @ProductID and assigned the value 1 to it. We defined conditions in the Case statement. When a condition is met, the value associated with it is returned.

Case Statements in SQL

Similarly, changing the condition in a SQL Case statement returns the correct expression. We want to acquire the Product name for ProductID 4 in the example below. It did not satisfy the Case statement condition, hence it returned the result of the Else expression.

Between two cases, you can’t use || operators. However, you don’t have to add a break between case values if you utilize numerous case values. The software will then move to the appropriate case and search for code to execute until it comes across a “break.”

As a result, the code for these scenarios will be the same. The SQL IN conditional (also known as the IN operator) makes it simple to see if an expression matches any element in a list. It’s utilized in SELECT, INSERT, UPDATE, and DELETE statements to help eliminate the need for multiple OR conditions. If you want to know 3 Ways to Add Javascript Code in HTML.

How to use JSON Array Literals?

Well here’s an addition to the knowledge meter for your growth. It’s the subject we’ll be delving into. So, to help you comprehend, let’s break down the name. For data transmission, JSON (JavaScript Object Notation) is utilized, and an array of strings is an ordered collection of values of the string type.

As a whole, the ‘JSON array of strings is an ordered list of values that can store numerous values. This can be used to save a string, a Boolean value, a number, or an object. Because we’re concentrating on an Array of Strings, let’s take a closer look at its syntax and a few samples.

A comma separates zero, one, or more ordered elements in a JSON array. The square brackets [] surround the JSON array. The first index of a JSON array is zero, indicating that it is zero-terminated (0).

  • JSON Array Literals
  • This is a JSON string:
  • ‘[“Hi”, “Hello”, “Greeting”]’

Here is a JSON array expression within the JSON thread:

  • [“Hi”, “Hello”, “Greeting”]

JSON arrays are nearly identical to JavaScript arrays.

Array values in JSON must be of the following types: string, number, object, array, boolean, or null.

Some of the above, or any acceptable JavaScript phrase, such as methods, dates, or unknown, can be used as array elements in JavaScript.

JavaScript Arrays

You can create a JavaScript array from a literal:


myArray = ["Hi", "Hello", "Greeting"];

After processing a JSON string, users can make a JavaScript array:


myJSON = '["Hi", "Hello", "Greeting"]';

myArray = JSON.Parse(myJSON);

Just saw how a JSON array represents an ordered set of values, whether they are strings, numbers, Booleans, or objects.

We’ve covered how to access an array of strings using a for loop, how to delete array values, multi-dimensional arrays, and nested arrays, with examples for each described above. Array values are retrieved using the index of each element in the JSON array, which is comparable to the JavaScript array.

Read More: How to convert python to javascript?


Please enter your comment!
Please enter your name here