Skip to content
Advertisement

Select something that has more/less than x character

Was wondering if it’s possible to select something that has more/less than x characters in SQL.

For example, I have an employee table and I want to show all employee names that has more than 4 characters in their name.

Here’s an example table

ID EmpName Dept
1  Johnny  ACC
2  Dan     IT
3  Amriel  PR
4  Amy     HR

Advertisement

Answer

If you are using SQL Server, Use the LEN (Length) function:

SELECT EmployeeName FROM EmployeeTable WHERE LEN(EmployeeName) > 4

MSDN for it states:

Returns the number of characters of the specified string expression,
excluding trailing blanks.

Here’s the link to the MSDN

For oracle/plsql you can use Length(), mysql also uses Length.

Here is the Oracle documentation:

http://www.techonthenet.com/oracle/functions/length.php

And here is the mySQL Documentation of Length(string):

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_length

For PostgreSQL, you can use length(string) or char_length(string). Here is the PostgreSQL documentation:

http://www.postgresql.org/docs/current/static/functions-string.html#FUNCTIONS-STRING-SQL

Advertisement