I my DB i want to select data which having A15-A19 using LIKE operator but couldnt get required result.
the code i made as SELECT * FROM MASTER_RULE WHERE VALUE BETWEEN LIKE 'A15%' AND 'A19%' and also tried regular expression as SELECT * FROM MASTER_RULE WHERE REGEXP_LIKE(value, 'A[1-9]') . But regexp gives all records not specified range 15-19.
How to achieve the solution for this?
Your first query is not ok, it has one extra keyword that you do not need.
Here is the regexp_like solution:
SELECT * FROM MASTER_RULE WHERE REGEXP_LIKE(value, '^A[1][5-9]')
Here is a demo
UPDATE:
Here is the "BETWEEN SOLUTION":
SELECT *
FROM MASTER_RULE
WHERE substr(value, 2,length(value)-1) between 15 AND 19
You could just use regular string comparisons:
where value >= 'A15' and
value < 'A20'
Not only is this simple, but the code can also take advantage of an index on value.
As you mentioned in the comments your data is like A15, A16, A17. etc you can achive your requirement with simple in clause also.
SELECT * FROM MASTER_RULE WHERE VALUE in ('A15','A16','A17','A18,'A19');
I have a simple select query on a table, but with different values in LIKE operator. Query is as follows:
SELECT *
FROM BatchServices.dbo.TaskLog
WHERE EntryTime BETWEEN '20190407' AND '20190408' AND
TaskGroup LIKE '%CSR%' AND
(LogText LIKE '%error%' OR LogText LIKE '%fail%')
This above query is fine and returning me the expected results but I don't want to have multiple LIKE in a query, so I have already tried something like
SELECT *
from BatchServices.dbo.TaskLog
WHERE taskgroup = 'csr' AND
LogText IN ( '%error%','%fail%') AND
EntryTime>'2019-04-07'
ORDER BY EntryTime ASC
This query is not giving me any results.
I am expecting a query which looks smarter than the one I have which returns result. Any help?
use like operator with OR condition
SELECT * from BatchServices.dbo.TaskLog WHERE taskgroup ='csr' AND
(LogText like '%error%' or LogText like '%fail%')
AND EntryTime>'2019-04-07'
ORDER BY EntryTime ASC
The LIKE operators are not the problem. It's the leading wild cards. Unless you cant get rid of those, your optimization options are going to be limited to making sure you have a covering index on EntryTime... That and replacing the "*" with the specific columns you need.
I have a table with about 200 million records. One of the columns is defined as varchar(100) and it's included in a full text index. Most of the values are numeric. Only few are not numeric.
The problem is that it's not working well. For example if a row contains the value '123456789' and i look for '567', it's not returning this row. It will only return rows where the value is exactly '567'.
What am I doing wrong?
sql server 2012.
Thanks.
Full text search doesn't support leading wildcards
In my setup, these return the same
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'28400')
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"2840*"')
This gives zero rows
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"*840*"')
You'll have to use LIKE or some fancy trigram approach
The problem is probably that you are using a wrong tool since Full-text queries perform linguistic searches and it seems like you want to use simple "like" condition.
If you want to get a solution to your needs then you can post DDL+DML+'desired result'
You can do this:
....your_query.... LIKE '567%' ;
This will return all the rows that have a number 567 in the beginning, end or in between somewhere.
99% You're missing % after and before the string you search in the LIKE clause.
es:
SELECT * FROM t WHERE att LIKE '66'
is the same as as using WHERE att = '66'
if you write:
SELECT * FROM t WHERE att LIKE '%66%'
will return you all the lines containing 2 'sixes' one after other
I have been working on this on for months. I just cannot get the natural (True alpha-numeric) results. I am shocked that I cannot get them as I have been able to in RPG since 1992 with EBCDIC.
I am looking for any solution in SQL, VBS or simple excel or access. Here is the data I have:
299-8,
3410L-87,
3410L-88,
420-A20,
420-A21,
420A-40,
4357-3,
AN3H10A,
K117GM-8,
K129-1,
K129-15,
K271B-200L,
K271B-38L,
K271D-200EL,
KD1051,
KD1062,
KD1092,
KD1108,
KD1108,
M8000-3,
MS24665-1,
SK271B-200L,
SAYA4008
The order I am looking for is the true alpha-numeric order as below:
AN3H10A,
KD1051,
KD1062,
KD1092,
KD1108,
KD1108,
K117GM-8,
K129-1,
K129-15,
MS24665-1,
M8000-3,
SAYA4008,
SK271B-200L
The inventory is 7800 records so I have had some problems with processing power as well.
Any help would be appreciated.
Jeff
In native Excel, you can add multiple sorting columns to return the ASCII code for each character, but if the character is a number, then add a large number to the code (e.g 1000).
Then sort on each of the helper columns, including the first column in the table, but not in the sort.
The formula:
=IFERROR(CODE(MID($A1,COLUMNS($A:A),1))+AND(CODE(MID($A1,COLUMNS($A:A),1))>=48,CODE(MID($A1,COLUMNS($A:A),1))<=57)*1000,"")
The Sort dialog:
The results:
You can implement a similar algorithm using VBA, and probably SQL also. I dunno about VBS or Access.
You could try using format for left padding the string in order by
select column
from my_table
order by Format(column, "0000000000")
Add a sorting column:
, iif (left(fieldname, 1) between '0' and '9', 1, 0) sortField
etc
order by sortField, FieldName
Lets say you have your data in column "A". If you put this formula in column "B" =IFERROR(IF(LEFT(A1,1)+1>0,"ZZZZZZZ "&A1,A1),A1), it will automatically add Z in front of all numerical values, so that they will naturally appear after all alphabetical values when you sort A-Z. later you can find&replace that funny ZZZZZZ string...
There a number of approaches, but likely the least amount of work is to build two columns that split out the delimiter (-) in this case.
You then “pad” the results (spaces, or 0) right justified, and then sort on the two columns.
So in the query builder we have this:
SELECT Field1,
Format(
Mid(field1,1,IIf(InStr(field1,"-")=0,50,InStr(field1,"-")-1)),
">##########") AS Expr1,
Format(
Mid(field1,IIf(InStr(field1,"-")=0,99,InStr(field1,"-")+1)),
">##########") AS Expr2
FROM Data
When we run the above raw query we get this:
So now in the query builder, simply sort on the first derived column, and then sort on the 2nd derived column.
Eg this:
Run the query, and we get this result:
Edit:
Looking at you desired results, it looks like above sort is wrong. We have to RIGHT just and pad with 0’s.
So this 2nd try:
SELECT Field1,
Left(Mid(field1,1,IIf(InStr(field1,"-")=0,30,InStr(field1,"-")-1))
& String(30,"0"),30) AS Expr1,
Left(Mid(field1,IIf(InStr(field1,"-")=0,99,InStr(field1,"-")+1))
& String(30,"0"),30) AS Expr2
FROM Data
The results are thus this:
Given your small table size, then the above query should perform quite well.
select
count(distinct [PROV_CT])
from
[HRecent]
where
[PROV_CT] not like ('%P125, %P961, %P160, %P960, %P220, %P004')
Can I write a query like this? Actually it is showing outputs which is different from the query output.
select
count(distinct [PROV_CT])
from
[HRecent]
where
[PROV_CT] not like '%P125' and
[PROV_CT] not like '%P220' and
[PROV_CT] not like '%P960' and
[PROV_CT] not like '%P004' and
[PROV_CT] not like '%P961' and
[PROV_CT] not like '%P160'
Can anyone help me out please? I want to write an optimised query.
You cannot write the query using a single string literal like in:
[PROV_CT] not like ('%P125, %P961, %P160, %P960, %P220, %P004')
This predicate doesn't look for separate values like '%P125', '%P961' etc.
If you have a very big list of values against which NOT LIKE operation is to be performed, then it might be simpler to do it like this:
select
count(distinct [PROV_CT])
from
[HRecent]
cross apply (
select count(*)
from (values ('%P125'), ('%P961'),
('%P160'), ('%P960'),
('%P220'), ('%P004') ) AS t(v)
where [PROV_CT] LIKE t.v) AS x(cnt)
where x.cnt = 0
Using VALUES Table Value Constructor you create an in-line table containing all the values against which [PROV_CTRCT] column is to be compared. Then query this table using a single LIKE operation to find if there is a match or not.
Demo here