LEN() Function SQL

For SQL applications other than mySQL, the LEN() function finds the length of the data in a character field (for a text field try DATALENGTH). The length is calculated in (bytes or characters) and does count blank spaces and special characters like quotation marks. The Syntax:

SELECT LEN(column) FROM table WHERE condition

The WHERE clause is optional, if left out an array of the specified columns of all the rows will be returned. In our table of fict_chars we might want to find out the length of the book title for each book. The table looks like this:

1Scarlet O'HaraGone With the Wind
2Hermione GrangerHarry Potter
3Jane PorterTarzan

We would make the following query:

SELECT fict_id, LEN(novel) AS nov_len FROM fict_chars

The result will be a table that looks like this:


Though this works in most SQL applications, it does not work in mySQL. To accomplish this same procedure to a character field it must be done using LENGTH. However, even LENGTH will not work for a TEXT field. For TEXT, TINYTEXT, etc. this operation will have to be done within the PHP interface with mySQL using the mysql_fetch_lengths() function.

< IFNULL Function SQL | MAKE_SET Function SQL >

Send notes in disappearing ink!

Interesting Pages