ORD Function in SQLThe ORD function of Structured Query Language shows the code of left-most character of the specified string or word. SQL also allows you to perform the ORD function on the String fields of the table. Syntax of ORD String FunctionSyntax1: This syntax uses the ORD function with the column names of the SQL table: In the syntax, we have to specify the column's name on which we want to use the ORD string function. Syntax2: This syntax uses the ORD function with the set of characters (string): Syntax2: This syntax uses the ORD function with the individual character: Examples of ORD String functionExample 1: The following SELECT query shows the ORD value of the given string: Output:
Example 2: The following SELECT query returns the ORD value of @ symbol: Output:
Example 3: The following SELECT query shows the ORD value of the first character of the given string: Output:
Example 3: The following SELECT query shows the ORD value of the 'R' character: Output:
Example 4: This example uses the ORD function with the SQL table In this example, we are going to create a new SQL table on which we have to execute the ORD function for finding the ORD value of first character. The syntax for creating the new table in the SQL database is as follows: The following CREATE statement creates the Student_Grade table: The below INSERT queries insert the records of students with grades and marks in the Student_Grade table: The following SELECT statement displays the inserted records of the above Student_Grade table:
Query 1: The following SELECT query uses the ORD function with the Last_Name column of the above Student_Grade table: This SQL statement shows the code of left-most character of the last name from the table. Output:
Query 2: The following SELECT query uses the ORD function with the First_City, Second_City, and New_City columns of those students whose Roll_No is greater than 2 in the above Student_Grade table: Output:
Next TopicDifferent Star Patterns in SQL |