FORMAT Function in SQLThis string function of Structured Query Language changes the format of the given text. In SQL, it can change the string format into any format. Syntax of FORMAT String FunctionSyntax1: This syntax uses the FORMAT function with the column name of the SQL table: In the syntax, we have to specify the name of those columns whose values we want to FORMAT. Syntax2: This syntax uses the FORMAT function with string: Examples of FORMAT String functionExample 1: The following SELECT query changes the string into a percentage: Output:
Example 2: The following SELECT query changes the string into currency amount: Output:
Example 3: The following SELECT query converts the string into the custom string format: Output:
Example 4: The following SELECT query converts the date into the specified format: Output:
Example 5: The following SELECT query converts the time in the specified format: Output:
Example 6: This example uses the FORMAT function with the table in Structured Query Language. In this example, we will create a new SQL table on which we want to execute the Format function. The syntax for creating the new table in the SQL database is as follows The following CREATE statement creates the Worker_Grade table: The below INSERT queries insert the records of Workers with Grades, Salaries, and Remarks in the Worker_Grade table: The following SELECT statement displays the inserted records of the above Worker_Grade table:
Query 1: The following SELECT query uses the FORMAT function with the Worker_ID column of the above Worker_Grade table: SELECT Worker_ID, Format(Worker_ID, C) AS Format_c FROM Worker_Grade; This SQL statement changes the format of the id of each worker. Output:
Query 2: The following SELECT query uses the FORMAT function with the Attendance_Remarks column of the above Worker_Grade table: This SQL statement changes the format of the Attendance remarks of each worker. Output:
Query 3: The following SELECT query uses the FORMAT function with the Worker_Salary column of the above Worker_Grade table: This SQL statement changes the format of the salary of each worker. Output:
Next TopicINSERT Function in SQL
|