How to Check a Column’s Data Type in SQL

In SQL, the columns information schema view, which returns information about columns, is an ISO standard view that is supported by most of the major RDBMSs. You can use this view to get information about a column’s data type.

Most of the major RDBMs also provide other methods for getting column information.

Here are examples of getting the data type of a column in various SQL databases.

Continue reading

How to Find Non-Numeric Values in a Column in MySQL

In MySQL, you can run a query like the following to return non-numeric data from the column.

This can be helpful if you ever find a column that contains numeric data, but it was set up as a varchar or char column. You can use this query to find any non-numeric values that might have been inserted into the column. You can then deal with it any way you like, such as convert them to numeric, then change the column type.

Continue reading

How to Format Numbers with Commas in SQL

Most of the major RDBMSs have functions that enable us to format numbers with commas as either the group separator, or the decimal separator.

Some RDBMSs also output certain numeric data types with commas in the relevant place.

Below are examples of formatting numbers with commas in some of the more popular DBMSs.

Continue reading

SQL LPAD()

In SQL, LPAD()is a commonly used function that pads the left part of a string with a specified character. The function can be used on strings and numbers, although depending on the DBMS, numbers may have to be passed as a string before they can be padded.

DBMSs that have an LPAD() function include MySQL, MariaDB, PostgreSQL, and Oracle.

DBMSs that don’t have an LPAD() function include SQL Server and SQLite (although there are other ways to apply left padding in these DBMSs).

Continue reading