Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select data items of a certain length?

Tags:

sql

How do I select the row of a column such that the row size is <= 5 ? Is there a query for this which will work on most/all databases ?

eg. id, first_name

Select only those people whose firstname is more than 10 characters. Their name is too long ?

like image 761
Use your head Avatar asked Jun 12 '13 09:06

Use your head


People also ask

How do I select a certain number of rows?

Select one or more rows and columns Or click on any cell in the column and then press Ctrl + Space. Select the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.

How do I find the length of a character in SQL?

LEN() function calculates the number of characters of an input string, excluding the trailing spaces. It is an expression that can be a constant, variable, or column of either character or binary data. Returns : It returns the number of characters of an input string, excluding the trailing spaces.


1 Answers

If you are bound to use a specific RDBMS then the solution is easy.

Use the LENGTH function.

Depending upon your database the length function can be LEN, Length, CarLength. Just search google for it.

According to your question

How do I select the row of a column such that the row size is <= 5 ? Is there a query for this which will work on most/all databases ?

solution can be

SELECT * FROM TableName WHERE LENGTH(name) <= 5

If you want something that can work with almost all the database and I assume that the length of your string that you want to fetch is of a significant small length. Example 5 or 8 characters then you can use something like this

 SELECT * 
 FROM tab
 WHERE
    colName LIKE ''
 OR colName LIKE '_' 
 OR colName LIKE '__'
 OR colName LIKE '___'
 OR colName LIKE '____'
 OR colName LIKE '_____'

This works with almost all major DBMS.

see example:

SQL Server

MySQL

Oracle

Postgre SQL

SQLite

like image 121
Ankit Suhail Avatar answered Sep 30 '22 18:09

Ankit Suhail