OCT Function in SQL

The OCT is a SQL string function which converts the given decimal number to its octal equivalent.

Syntax of OCT Function

In the OCT syntax, we have to pass that decimal number whose octal equivalent we want to find.

In the Structured Query Language, we can also use the OCT function with the column of the table as shown in the following block:

In this syntax, we have to define the name and column of that table on which we want to perform the OCT function.

Examples of OCT function

Example 1: This example returns the octal representation of the specified number:

Output:

octal_of_101
145

Example 2: This example returns the octal representation of the specified number:

Output:

octal_of_2
2

Example 3: This example returns the representation of 8:

Output:

octal_of_8
10

Example 4: This example returns the octal representation of 255:

Output:

octal_of_255
377

Example 5: This example returns the octal representation of NULL:

Output:

octal_of_NUL
NULL

Example 6: This example returns the octal representation of NULL:

Output:

21222324252627282930
25262730313233343536

Example 7: This example uses the OCT function with the SQL table.

In this example, we will create the new table through which we will perform the OCT function on the columns of the table:

The following shows the syntax to create the new table in SQL:

The following CREATE statement creates the Product_Details table for storing the price and quantity of products:

The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table:

The following SELECT statement displays the inserted records of the above Product_Details table:


Product_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateProduct_Rating
104P110.250945NULL2022-04-30NULL
202P415.50045752022-01-285
103P218.25025NULL2022-02-184
111P725.2505152021-12-259
210P615.50050702021-10-15NULL
212P819.7501102502022-01-284
112P1010.2505508352022-04-11NULL

Query 1: The following SELECT query uses the OCT function with the Product_Quantity column of the above Product_Details table:

This query shows the octal representation of product id of each product.

Output:

Product_IDoctal_of_Product_ID
104150
202312
103147
111157
210322
212324
112160

Query 2: The following SELECT query uses the OCT function with Selling_Price column of the above Product_Details table:

This query shows the octal representation of purchasing and selling price of each product.

Output:

Selling_Priceoctal_of_SellingPrice
NULLNULL
75113
NULLNULL
1517
70106
250372
8351503

Query 3: The following SELECT query uses the OCT function with the Product_Rating column of the above Product_Details table:

This query shows the octal representation of rating of each product from the above table.

Output:

Product_Ratingoctal_of_productrating
NULLNULL
55
44
911
NULLNULL
44
NULLNULL





Latest Courses