How to use wildcard characters in this? - sql

I have a table of this manner:
+---------+--------------------+
|ID | Component |
+---------+--------------------+
|00241147 | 000000001000245598 |
|00241147 | 000000001000090069 |
|00249207 | 000000002510256707 |
|00249208 | 000000002510245146 |
+---------+--------------------+
I want to select only those rows where Component is starting with '1'.
I'm using the following code:
select * from Table where Component like '%1%'

Cast them as bigint and read 1 using left() function
select * from Table where left(cast(Component as bigint), 1) = 1
Note : This above assumes that Component column has varchar datatype
EDIT : Thanks for making demo for clarification by Uwe Keim
http://sqlfiddle.com/#!6/1987d/4

Try this:
SELECT * FROM Table
WHERE SUBSTRING(str_col, PATINDEX('%[^0]%', str_col+'.'), LEN(str_col)) LIKE '1%';

Related

HIVE SQL: Select rows whose values contain string in a column

I want to select rows whose values contain a string in a column.
For example, I want to select all rows whose values contain a string '123' in the column 'app'.
table:
app id
123helper xdas
323helper fafd
2123helper dsaa
3123helper fafd
md5321 asdx
md5123 dsad
result:
app id
123helper xdas
2123helper dsaa
3123helper fafd
md5123 dsad
I am not familiar with SQL query.
Could anyone help me? .
Thanks in advances.
In a number of ways:
like:
select * from table
where app like '%123%'
rlike:
...
where app rlike '123'
instr:
...
where instr(app, '123')>0
locate:
...
where locate('123', app)>0
Invent your own way.
Read manual: String Functions and Operators.
Try the following using like
select
*
from yourTable
where app like '%123%'
Output:
| app | id |
| ---------- | ---- |
| 123helper | xdas |
| 2123helper | dsaa |
| 3123helper | fafd |
| md5123 | dsad |
Please use below query,
select app, id from table where app like '%123%';
Below are few additional information,
like '123%' --> Starts with 123
like '%123' --> Ends with 123
like '%123%'--> Contains 123 anywhere in the string

how to use sql where condition on comma separated value

| id | name | numbers |
+----+---------+----------------+
| 1 | arjun | 62,45,68,95,50 |
| 2 | yuvaraj | 45,65,85,68 |
| 3 | sahadev | 45,65,85,68 |
| 4 | yogi | 45,65,85,68 |
| 5 | krishna | 45,65,85,68 |
I want id data where number is 45
I tried
select *
from table
where number=45
but it doesn't work.
With like:
select * from table where concat(',', numbers, ',') like concat('%,', '45',',%')
because you need to transform the column's value to something like this:
,45,65,85,68,
and then apply like to the pattern '%,45,%'.
You can try using like operator
select * from table where number like '%45%'
or you can use find_in_set() for mysql database
select * from table where find_in_set(45,number)>0
using like operator with pattern as '%45%':
select * from table where numbers like '%45%'
For Postgres you can use:
select *
from the_table
where '45' = any(string_to_array(numbers, ','));
If you use like operator it will get all the records having 45 (like 345 or 12245 or 65445). if you want records having values exactly "45" You can try the below code if you are using SQL Server 2016 or higher versions.
select * from (
SELECT * FROM tablename
cross apply
STRING_SPLIT('numbers',',') )a where value='45'

Oracle SQL - Substring issue

I have an field pattern and value in that field is INDI/17-18/6767/KER/787 .I want to get 6767 from this string
I used the query
select substr(pattern,12,15) from pattern_table
But the output I got is 6767/KER/787 instead of 6767.
Try this:
You have to give the length as the 3rd value, not the position.
SELECT SUBSTR(pattern,12,4) FROM pattern_table
For a generic result to get the 3rd value separated by a delimiter, you may use REGEXP_SUBSTR.
SQL Fiddle
Query 1:
SELECT pattern,REGEXP_SUBSTR(pattern, '[^/]+', 1, 3) id
FROM pattern_table
Results:
| PATTERN | ID |
|--------------------------|-------|
| INDI/17-18/6767/KER/787 | 6767 |
| INDI/17-18-19/67/KER/787 | 67 |
| INDI/16-18/67890/KAR/986 | 67890 |
even this will also work:
SELECT substr('INDI/17-18/6767/KER/787',instr('INDI/17-
18/6767/KER/787','/',1,2)+1,4) FROM dual;

Search an SQL table that already contains wildcards?

I have a table that contains patters for phone numbers, where x can match any digit.
+----+--------------+----------------------+
| ID | phone_number | phone_number_type_id |
+----+--------------+----------------------+
| 1 | 1234x000x | 1 |
| 2 | 87654311100x | 4 |
| 3 | x111x222x | 6 |
+----+--------------+----------------------+
Now, I might have 511132228 which will match with row 3 and it should return its type. So, it's kind of like SQL wilcards, but the other way around and I'm confused on how to achieve this.
Give this a go:
select * from my_table
where '511132228' like replace(phone_number, 'x', '_')
select *
from yourtable
where '511132228' like (replace(phone_number, 'x','_'))
Try below query:
SELECT ID,phone_number,phone_number_type_id
FROM TableName
WHERE '511132228' LIKE REPLACE(phone_number,'x','_');
Query with test data:
With TableName as
(
SELECT 3 ID, 'x111x222x' phone_number, 6 phone_number_type_id from dual
)
SELECT 'true' value_available
FROM TableName
WHERE '511132228' LIKE REPLACE(phone_number,'x','_');
The above query will return data if pattern match is available and will not return any row if no match is available.

Use function result inside a select statement

I have in oracle a simple select statement (example):
SELECT * FROM organisation WHERE ID=15
This returns a row :
**ID | NAME | NOTES | VALUE**
15 | BEST | Just Notes...| 112
Now I want to take the Value (112) and use it as a parameter in an oracle function :
function get_session_text (
in_value in number
)
return varchar2 is....
So I would like to build a select statement that it will return something like that:
**ID | NAME | NOTES | VALUE | TEXT **
15 | BEST |Just Notes... | 112 | function's result
Select * from
I tried to build it but I am not familiar with functions, so could you please help me with that SQL statement?
SELECT id,
name,
notes,
value,
get_session_text( value )
FROM organization
should do it.