SQL - Counting rows based on specific digit value in a field - sql

I am using Oracle database.
In my table t_mytable, I have one field myfield and this field has string values like 00101110010.
I need to count the rows which has ""4th digit of myfield value is 1".
For instance,
myfield
-------
00101110010
00111110010
00101101010
00101110010
00111111110
For above data, count should be 2 because 2 rows has fourth bit as 1 (I started from 1 not 0 while determining first digit).
How can I do this in sql?

if myfield is a string you can use substr for extract the fourth char
select count(*)
from t_mytable
where substr(myfield, 4,1) ='1';

Related

Find Max Value in string/integers in a column in SQL

I have a table with a column UniqueID of type varchar. This column has unique IDs labeled as follows:
DU19F0001
DU19M001
DU19M002
DU19F002
EL19F001
EL19F002
MU19M001
MU19M002
I am trying to select for the last max value based on this mixed string. For instance what is the last value for 'DU' '19' 'F'? The result should then be DU19F002. How do I write a query for selecting the max value based on a mix of strings and integers in a column?
Is this what you want?
select max(uniqueid)
from t
where uniqueid like 'DU19F%';
If you want to do this for the first 5 (or whatever) characters, you can do this:
select
first_part,
max(uniqueid)
from
(
select substring(uniqueid,1,5) as first_part,uniqueid from <your table> ) t
group by first_part

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.

WHERE varchar = variable length of 0's

Table_A
ID Number
-- ------
1 0
2 00
3 0123
4 000000
5 01240
6 000
The 'Number' column is data type varchar.
EDIT for clarity.
My question is, can I easily pull back all rows of data which contain a variable length string of 0's?
I have tried:
SELECT *
FROM Table_A
WHERE LEFT(Number,1) = '0' AND RIGHT(Number,1) = '0'
Using the above, it would still return the below, using the example table provided.
ID Number
-- ------
1 0
2 00
4 000000
5 01240
6 000
I was looking for a function which I could pass the LEN(Number) int into, and then it generates a string of a specfic character (in my case a string of 0's). I wasn't able to find anything though.
Oh, and I also tried adding a SUBSTRING to the WHERE clause, but sometimes the Number column has a number which has a 0's in the middle, so it still returned strings with other numbers except only 0.
SUBSTRING(Number,ROUND(LEN(Number)/2,0),1) = '0'
Any help is appreciated.
So, you want a string that doesn't contain anything that isn't a 0? Sounds like it's time for a double-negative:
SELECT *
FROM Table_A
WHERE NOT Number like '%[^0]%'
AND number like '0%' --But we do want it to contain at least one zero
(The final check is so that we don't match the empty string)
Answer:
Where number like '%0%'
Your can use this query :
SELECT * FROM Table_A WHERE Number LIKE '%0%';
It'll solve your problem.
SELECT *
FROM yourtable
WHERE len(Number) - len(replace(number,'0','')) >= 0
One more approach
You can use this following one also,you will get your expected result.
SELECT *
FROM Table_A
WHERE Nunber not like '%[1-9]%'
Thanks.

Word (Along with space) search in T Sql

I have a column Transactions in a SQL Server table A.
I have around 1000 rows in that table, and in each row I have strings which has combination of
"#transaction --space of 3 characters---1" or "#transaction --space of 3 characters---0" these strings may be even repeated n number of times within the single row.
Result - I want to check all the
"#transaction --space of 3 characters---1" ones how many times repeated.
"#transaction --space of 3 characters---0" zeros how many times repeated.
I tried with queries such as
Select * from A where transaction like '%#transaction --space of 3 characters---1%
Select * from A where transaction like '%#transaction --space of 3 characters---0%
But these can't give me the desired result because there may be more number of above strings in single column.
Can anyone suggest how to count all these two strings with ending 1 and 0 separately ?
Thanks in advance
Try this
SELECT count (*) as [Count], 'Strings with 1' as [Comment]
FROM A
WHERE transaction like cast (#transaction as varchar(50)) +'???1%'
UNION ALL
SELECT count (*) as [Count], 'Strings with 0'
FROM A
WHERE transaction like cast (#transaction as varchar(50)) +'???0%'

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