T - SQL statement for number ranges - sql

Create table temp
(
ID nvarchar(50)
)
ID contains numeric values prevailing zeros in some cases so it is defined as varchar
How to get values starts with 3555 to 3999 and 8000 to 9999.There is no specific rule that length is always 4.
Eg:
3555
35688888888888
3590909
8000
85805667
all of the values are valid and are to be fetched.
Please let me know T- SQL statement for the above scenario

You can use few expressions with LIKE. If you have an index on ID, it would use it, so it will be efficient. Something like this:
SELECT
ID
FROM
temp
WHERE
ID LIKE '3[5-9]%'
OR ID LIKE '[89]%'
LIKE '3[5-9]%' matches any string that starts with 3 and which second character is 5 or 6 or 7 or 8 or 9. After these two characters there can be 0 or more other characters. Any number of extra characters.
LIKE '[89]%' matches any string that starts with 8 or 9 and any number characters after.

You can extract the first four chars, convert that to a number and query like this:
SELECT
[ID]
FROM temp
WHERE convert(int,LEFT([ID],4)) BETWEEN 3500 AND 3999
OR convert(int,LEFT([ID],4)) BETWEEN 8000 AND 9999
For lots of data this will be horribly slow, so if you need performance I would recommend to add an indexed int column to the table where you store the number that represents the first four digits of ID.

Related

SQL get numbers that contain certain digits

How do I get number field that contain 878 as reflected in the result. It should contain two 8 and one 7.
Edit : using TSQL
Number
878
780
788
700
Result
878
788
There are only 3 combinations so what about the crudest of them all?
Number = 788 OR Number = 878 OR Number = 887
Or even:
Number IN (788,878,887)
If numbers are not just 3 digits then cast the number as string and then:
NumberAsString LIKE '%887%' OR NumberAsString LIKE '%878%' OR NumberAsString LIKE '%788%'
A three digit number consisting of exactly one seven and two eights:
where number in (788, 878, 887)
Everything else would be overkill for this simple task.
If the task were different, say, the number can have more digits and must contain exactly one seven and two eights, then we could use an appropriate combination of LIKE and NOT LIKE to get what we are looking for. E.g:
where number like '%7%' -- contains a 7
and number like '%8%8%' -- contains two 8s
and number not like '%7%7%' -- doesn't contain more than one 7
and number not like '%8%8%8%' -- doesn't contain more than two 8s
UPDATE:
This is not a good solution, in stead I would go for the next solution suggested here.
If you are using MySQL REGEX is your friend:
SELECT * FROM _TABLE_
WHERE `Number` REGEXP "[7]{1}" AND `Number` REGEXP "[8]{2}";
More info: https://dev.mysql.com/doc/refman/8.0/en/regexp.html
I think in SQL Server should be something like this:
SELECT * FROM _TABLE_
WHERE `Number` LIKE '%[7]{1}%' AND `Number` LIKE '%[2]{1}%';
More info: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15

How do you do multiple substrings for a field in Teradata?

I have a field to pull account numbers which have different lengths and I want to pass the last four digits of the account number. The dilemma I am having is that since they are different lengths I am having trouble in substringing the fields. The standard length is 11 digits but there are accounts with 9 digits and 7 digits.
How do I substring those values in multiple substrings to capture all the account last 4 digits in one query?
This currently what I have:
SELECT SUBSTRING(ACCT_NBR,7,4) AS BNK_ACCT_NBR
FROM NAMEOFTABLE;
I want to have additional substrings to capture the account numbers that don't have 11 digits similar to
SUBSTRING(ACCT_NBR,5,4)
SUBSTRING(ACCT_NBR,4,4)
The results should look like:
76587990891 - 0891
654378908 - 8908
45643456 - 3456
Can you please help me in figuring out how I can do that?
Thanks.
Is ACCT_NBR a VarChar or an INT?
VarChar:
Right (ACCT_NBR,4)
Substr(ACCT_NBR,Char_Length(x)-3)
INT:
ACCT_NBR MOD 10000

SQLite3 Order by highest/lowest numerical value

I am trying to do a query in SQLite3 to order a column by numerical value. Instead of getting the rows ordered by the numerical value of the column, the rows are ordered alphabetically by the first digit's numerical value.
For example in the query below 110 appears before 2 because the first digit (1) is less than two. However the entire number 110 is greater than 2 and I need that to appear after 2.
sqlite> SELECT digit,text FROM test ORDER BY digit;
1|one
110|One Hundred Ten
2|TWO
3|Three
sqlite>
Is there a way to make 110 appear after 2?
It seems like digit is a stored as a string, not as a number. You need to convert it to a number to get the proper ordering. A simple approach uses:
SELECT digit, text
FROM test
ORDER BY digit + 0

Extracting number of specific length from a string in Postgres

I am trying to extract a set of numbers from comments like
"on april-17 transactions numbers are 12345 / 56789"
"on april-18 transactions numbers are 56789"
"on may-19 no transactions"
Which are stored in a column called "com" in table comments
My requirement is to get the numbers of specific length. In this case length of 5, so 12345 and 56789 from the above string separately, It is possible to to have 0 five digit number or more more than 2 five digit number.
I tried using regexp_replace with the following result, I am trying the find a efficient regex or other method to achieve it
select regexp_replace(com, '[^0-9]',' ', 'g') from comments;
regexp_replace
----------------------------------------------------
17 12345 56789
I expect the result to get only
column1 | column2
12345 56789
There is no easy way to create query which gets an arbitrary number of columns: It cannot create one column for one number and at the next try the query would give two.
For fixed two columns:
demo:db<>fiddle
SELECT
matches[1] AS col1,
matches[2] AS col2
FROM (
SELECT
array_agg(regexp_matches[1]) AS matches
FROM
regexp_matches(
'on april-17 transactions numbers are 12345 / 56789',
'\d{5}',
'g'
)
) s
regexp_matches() gives out all finds in one row per find
array_agg() puts all elements into one array
The array elements can be give out as separate columns.

SQL Select where id is in `column`

I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id