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

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

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)

Query to find results starting with a number or higher - SQL Server

Example: http://sqlfiddle.com/#!18/7f3df/2
CREATE TABLE Table1 (
Reg uniqueidentifier
);
INSERT INTO Table1 (Reg)
Values
('DF215E10-8BD4-4401-B2DC-99BB03135F2E'),
('93015E10-8BD4-4401-B2DC-99BB03135F2E'),
('21215E10-8BD4-4401-B2DC-99BB03135F2E');
Query:
select * from Table1
WHERE left(CAST(Reg AS CHAR(36)),1) > 8
I need to create a query that finds all results that start with either a number 8 or higher or starts with a letter but i am getting a conversion error i cant find a fix for. The column is a uniqueidentifier. How can i cast this to something i can filter on?
Can anyone give some advice on a solution to this?
Thanks
You need to do the comparison using strings:
convert(varchar(36), newid()) like '[89ABCDEF]%'
You may use SQL Server's enhanced LIKE here, which has some basic regex support:
SELECT *
FROM table1
WHERE Reg LIKE '[89A-Z]%';
The pattern [89A-Z]% says to match a starting 8 or 9, or any letter.

SQL statement to get largest ID from database column

I have an ID column which it supposed to set to auto-increment but I forgot to set in when creating the database. Let's say the ID is from 1 - 20. I used the select Max() Sql statement to get the largest ID:
SELECT MAX(id) FROM Table_Name;
It supposed to return me 20. However, it returns me 9. I also realized that the id column in database is jumbled up. It starts from 1,2 then skips to 9,10 - 20 then back to 3 - 8. And 8 appears to be the last row and I think that's where the 9 comes from. My id in database is varchar() data type.
So, is there any way to amend my Sql statement to get the largest id in a list of sorted id?
Thanks in advance.
The issue is likely that the ID column is a varchar field, so 9 is greater than 10.
select max(convert(int, id)) from Table
Your column is a character type, not a numeric type, which explains everything you're seeing.
Try casting it to numeric:
select max(cast(id as signed)) from table
You haven't said which database you are using, so the syntax may vary to achieve the cast - consult online docs for your database.
Try this:
SELECT TOP 1 Id FROM Table ORDER BY Convert(INT, id) DESC

How to select data items of a certain length?

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

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;