like search on number column in SQL - sql

How do I do a like search on a number column in SQL?
I want numbers which are like '0.0000%'.
I tried with
select * from emp where emp_id & '' like '123%'
select * from emp where CONVERT(varchar(20), emp_id) like '123%'
but in vain.
Please help me

Regardless of which DBMS you are using AND assuming you have a valid reason to do this, you have several ways to solve problems like these. I can think of three right now:
Convert the number to a string and use a LIKE operator on this:
select *
from emp
where to_char(emp_id) like '123%';
Use mathematical operators directly (like Andrey suggests), for example:
select *
from table
where num between 0 and 0.0001;
Construct a mathematical expression (actually, this is just another case of method 2), for example:
select *
from table
where abs(num - round(num, 5)) < 0.00001;

Use comparison operators (> and <):
select * from table where num > 0 and num < 0.00001

select 0.0001*1000 from dual if it is <1 means the 0.0001 has 3 or more zeros.
so i did like
select * from emp where emp_id*10000<1

Related

How to get first 9 digits of a column in openquery?

I am using SSMS to pull some data from oracle via openquery and only need the first 9 digits of a number out of a column.
I have tried using "left(column1, 9)" and it returns "LEFT:invalid identifier"
SELECT *
FROM OPENQUERY(servername,'
SELECT left(sv.column2, 9) AS new_number
FROM server.servername sv
')
column2 = 0987654321
new_number = 098765432
You miss the "Select"
SELECT *
FROM OPENQUERY([SERVER\INSTANCE],' select left(sv.column, 9) AS new_number FROM BD.dbo.Table sv')
Good Code.
In Oracle, you would use substring(), so something like this:
SELECT SUBSTR(sv.column2, 1, 9) AS new_number
FROM server.servername sv

Select Where Like regular expression

I need to create a SQL Query.
This query need to select from a table where a column contains regular expression.
For example, I have those values:
TABLE test (name)
XHRTCNW
DHRTRRR
XHRTCOP
CPHCTPC
CDDHRTF
PEOFOFD
I want to select all the data who have "HRT" after 1 char (value 1, 2 and 3 - Values who looks like "-HRT---") but not those who might have "HRT" after 1 char (value 5).
So I'm not sure how to do it because a simple
SELECT *
FROM test
WHERE name LIKE "%HRT%"
will return value 1, 2, 3 and 5.
Sorry if I'm not really clear with what I want/need.
You can also change the pattern. Instead of using % which means zero-or-more anything, you can use _ which means exactly one.
SELECT * FROM test WHERE name like '_HRT%';
You can use substring.
SELECT * FROM test WHERE substring(name from 2 for 3) = 'HRT'
Are the names always 7 letters? Do:
SELECT substring (2, 4, field) from sometable
That will just select the 2-4th characters and then you can use like "%HRT"

Search (in Oracle), using wild card without worrying about the case or order in which the words appear

How to search using wild card (in Oracle), without worrying about case or order in which the words appear.
e.g. if I search for like '%a%b%', it should return values containing *a*b*, *A*B* ,*b*a* and *B*A* This is just a sample, the search may have 5 or more words, is it possible get the result in just one expression rather than using AND.
select *
from yourtable
where yourfield like '%a%'
and yourfield like '%b%'
Or you can investigate Oracle Text
select * from table
where upper(column) like '%A%B%';
or
select * from table
where lower(column) like '%a%b%';
or
select * from table
where upper(column) like '%A%'
and upper(column) like '%B%';
or
select * from table
where lower(column) like '%a%'
and lower(column) like '%b%';
However, if you would like to find out *a*b* or *b*a* and so on. The other solution would be to sort the CHARS in the VARCHAR first i.e. if you have a string like aebcd then sort it to abcde and then do pattern match using like. As per this you can use below query to sort the chars in a varchar and then do the pattern match on it.
SELECT 1 val
FROM (SELECT MIN(permutations) col
FROM (SELECT REPLACE (SYS_CONNECT_BY_PATH (n, ','), ',') permutations
FROM (SELECT LEVEL l, SUBSTR ('cba', LEVEL, 1) n
FROM DUAL --replace dual by your table
CONNECT BY LEVEL <= LENGTH ('cba')) yourtable
CONNECT BY NOCYCLE l != PRIOR l)
WHERE LENGTH (permutations) = LENGTH ('cba')) temp_tab
WHERE upper(col) like '%A%B%';
Returns
val
------------------
1

How I can get the first 3 digits in 123456 Numbers in sql?

I have field called CallingParty in My CDR table it contains data like this:
CallingParty
------------
267672668788
I want to select the first 3 number of each of those numbers like
CallingParty
------------
267
if CallingParty is of type int:
SELECT CAST(LEFT(CallingParty, 3) AS INT)
From CDR
SQL Server has a Left() function, but it works best on strings. (varchar/char in SQL)
Select left(cast(267672668788 as varchar), 3)
Use this query:
SELECT SUBSTRING(CAST(CallingParty AS VARCHAR(50)), 1, 3) FROM [CDR]
If the data length does not change then you can always divide by 10 * the digits you have
SELECT FLOOR(267672668788 / 1000000000)
=267
Try this:
SELECT Substring(callingparty, 1, Length(callingparty) - 9)
FROM cdr;

Searching Technique in SQL (Like,Contain)

I want to compare and select a field from DB using Like keyword or any other technique.
My query is the following:
SELECT * FROM Test WHERE name LIKE '%xxxxxx_Ramakrishnan_zzzzz%';
but my fields only contain 'Ramakrishnan'
My Input string contain some extra character xxxxxx_Ramakrishnan_zzzzz
I want the SQL query for this. Can any one please help me?
You mean you want it the other way round? Like this?
Select * from Test where 'xxxxxx_Ramakrishnan_zzzzz' LIKE '%' + name + '%';
You can use the MySQL functions, LOCATE() precisely like,
SELECT * FROM WHERE LOCATE("Ramakrishnan",input) > 0
Are the xxxxxx and zzzzz bits always 6 and 5 characters? If so, then this is doable with a bit of string cutting.
with Test (id,name) as (
select 1, 'Ramakrishnan'
union
select 2, 'Coxy'
union
select 3, 'xxxxxx_Ramakrishnan_zzzzz'
)
Select * from Test where name like '%'+SUBSTRING('xxxxxx_Ramakrishnan_zzzzz', 8, CHARINDEX('_',SUBSTRING('xxxxxx_Ramakrishnan_zzzzz',8,100))-1)+'%'
Results in:
id name
1 Ramakrishnan
3 xxxxxx_Ramakrishnan_zzzzz
If they are variable lengths, then it will be a horrible construction of SUBSTRING,CHARINDEX, REVERSE and LEN functions.