How to select data items of a certain length? - 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 ?

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

Assuming you want the length in characters, the function names vary with RDBMS;
MySQL: CHAR_LENGTH().
Oracle: LENGTH().
SQL Server: LEN().
PostgreSQL: CHAR_LENGTH() or LENGTH().
SQLite: LENGTH().
If you want the length in bytes, it's instead;
MySQL: LENGTH().
Oracle: LENGTHB().
SQL Server: DATALENGTH().
PostgreSQL: OCTET_LENGTH().
For example, selecting all rows with names longer than 10 characters in MySQL would be;
SELECT * FROM myTable WHERE CHAR_LENGTH(name) > 10;

In sql servers TSQL you could use the len function.
eg
SELECT * FROM people WHERE LEN(firstname) > 10
Where people is the table name.
In mysql the function is called "Length" instead of len.

In case you call from Java, there is portable JDBC escape function:
select {fn length('some text expression')} from sometable

Related

Filter unwanted characters from a column in Teradata

I have a Phone number column in my table with values only being numbers and no special characters. for one of the column I got a value coming in as ":1212121212".
I will need to filter this record and any records coming in with any special characters in teradata. Can anyone help on this.
I have tried the below solutions but it is not working
where (REGEXP_SUBSTR(column_name, '[0-9]+')<>1 or column_name is null )
In MS SQL Server DB's, you can use TRYCAST to find those entries having non numeric characters:
SELECT column_name
FROM yourtable
WHERE TRY_CAST(column_name AS INT) IS NULL;
In Teradata DB's, you can use TO_NUMBER:
SELECT column_name
FROM yourtable
WHERE TO_NUMBER(column_name) IS NULL;
If you want to stay close to your attempt, can use LIKE to find not numeric entries:
SELECT column_name
FROM yourtable
WHERE column_name LIKE '%[^0-9]%';
Note this could get slow when your table has very many rows.
Thanks Jonas. Since I need only numeric values and the length should be 10, I tried the below and it worked. This would ignore all the additional special characters.
(regexp_similar(Column,'[0-9]{10}')=1)

How to write a sql to get certain column of data with certain substring or starting characters

My question is: is there any in-build standard sql function to make such task easier?
Thanks in advance!
Oracle supports the LIKE operator and the % wildcard. (Also the _ wildcard.) To find all rows that begin with "abc" you could use:
SELECT * FROM MyTable Where MyColumn LIKE 'abc%'
To find all rows that contain the substring "abc" somewhere within MyColumn, you could use:
SELECT * FROM MyTable Where MyColumn LIKE '%abc%'
Check out this helpful page.

Max number in a sql table column which starts with specific number

Can somebody help me to find the max number in a SQL table column which starts with a number (ex: 901)
My table column may contain number starts with any digit. So I just need to find the maximum number of all numbers which start with 901 (my number is like length of 9 digits)
SELECT MAX(column_name) FROM table_name;
This will give the maximum number of all.
Thanks
SELECT MAX(column_name) FROM table_name where column_name => 901000000
I think would run a lot faster than converting every value in the result set to a string and then using a like clause on it
this can work, but there might be a better solution too
select MAX(column_name) FROM
(select * from table_name where to_char(column_name) like '901%' )
I am not very familiar with the MS SQL functions - CAST or CONVERT? can be used

Retrieve the maximum length of a VARCHAR column in SQL Server

I want to find the longest VARCHAR in a specific column of a SQL Server table.
Here's an example:
ID = INT IDENTITY
DESC = VARCHAR(5000)
ID | Desc
---|-----
1 | a
2 | aaa
3 | aa
What's the SQL to return 3? Since the longest value is 3 characters?
Use the built-in functions for length and max on the description column:
SELECT MAX(LEN(DESC)) FROM table_name;
Note that if your table is very large, there can be performance issues.
For MySQL, it's LENGTH, not LEN:
SELECT MAX(LENGTH(Desc)) FROM table_name
Watch out!! If there's spaces they will not be considered by the LEN method in T-SQL. Don't let this trick you and use
select max(datalength(Desc)) from table_name
For Oracle, it is also LENGTH instead of LEN
SELECT MAX(LENGTH(Desc)) FROM table_name
Also, DESC is a reserved word. Although many reserved words will still work for column names in many circumstances it is bad practice to do so, and can cause issues in some circumstances. They are reserved for a reason.
If the word Desc was just being used as an example, it should be noted that not everyone will realize that, but many will realize that it is a reserved word for Descending. Personally, I started off by using this, and then trying to figure out where the column name went because all I had were reserved words. It didn't take long to figure it out, but keep that in mind when deciding on what to substitute for your actual column name.
Gives the Max Count of record in table
select max(len(Description))from Table_Name
Gives Record Having Greater Count
select Description from Table_Name group by Description having max(len(Description)) >27
Hope helps someone.
For SQL server (SSMS)
Option 1:
-- This returns number of characters
select MAX(LEN(ColumnName)) from table_name
Option 2:
-- This returns the number of bytes
select MAX(DATALENGTH(ColumnName)) from table_name
If you're using VARCHAR, use DATALENGTH. More details
SELECT TOP 1 column_name, LEN(column_name) AS Lenght FROM table_name ORDER BY LEN(column_name) DESC
Many times you want to identify the row that has that column with the longest length, especially if you are troubleshooting to find out why the length of a column on a row in a table is so much longer than any other row, for instance. This query will give you the option to list an identifier on the row in order to identify which row it is.
select ID, [description], len([description]) as descriptionlength
FROM [database1].[dbo].[table1]
where len([description]) =
(select max(len([description]))
FROM [database1].[dbo].[table1]
SELECT MAX(LEN(Desc)) as MaxLen FROM table
select * from table name from where length( column name) =(select MAX(Length(column name) from table name);
I am using subquery its 100 % work try this.
For IBM Db2 its LENGTH, not LEN:
SELECT MAX(LENGTH(Desc)) FROM table_name;

What SQL-server function can I use to get the character or byte length of a nvarchar(max) column?

I have a column which is of type nvarchar(max). How do I find the length of the string (or the number of bytes) for the column for each row in the table?
SELECT LEN(columnName) AS MyLength
FROM myTable
If you want to find out the max there should be a way for you to get the schema of the table. Normally you can do something like SHOW COLUMNS in SQL or a DESCRIBE style command. In a mysql shell that can be shortened to:
desc tablename;
Then if you want to determine the length of a string there is normally a function like LENGTH (for bytes) or CHAR_LENGTH (for characters).
SELECT *, LENGTH(fieldname) AS len FROM tablename
SELECT LEN(columnName) AS MyLength FROM myTable
I used this query for my table. It displays the size of each row in a particular column.
I need that field name size maximum it allow the characters.