Archive for February 15th, 2012

The MID() Function in SQL

Okay, you are given the MAX () function which finds the largest item in a column. You get a hold of the MIN() function which finds the lowest number in a column. Now, you are handed the MID() function as an SQL programmer. What does the MID() function do? Trick question, I know. You are right it does NOT find the middle number in a column.

The MID() function in SQL is used to extract a substring from a larger string. Syntax:

MID(column_or_string, beginning_position, length)

The column or string parameter is the string to be looked at. The beginning position is where to start. Each character position will have a number beginning with 1 (other languages in similar functions will often begin at zero). The length is optional; it tells how many characters, including the first one, are to be included in the substring to be extracted. The length parameter is optional. If left off, the beginning position through the remainder of the string will be the result. This function would be a good way to, for example, come up with the last four digits of a credit card number. For example:

SELECT MID(’1234567890654321′, 13, 4)

Will render: “4321″.

What a handy SQL function.

No Comments