Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I 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 have 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

1 Answer

0 votes
by (40.7k points)

 You can try using the LEN (Length) function if you are using SQL Server like this:

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: https://docs.microsoft.com/en-us/sql/t-sql/functions/len-transact-sql?view=sql-server-2017

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

Have a look at this for the Oracle documentation:

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

If you are using PostgreSQL, then you can use length(string) or char_length(string). For more information, refer to this PostgreSQL documentation:

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

For the MySQL Documentation of Length(string), you can refer to this link:

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

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...