I have an sql issue using teradata sql-assistant with like operator as is shown in the below exemple:
table A
id|
23_0
111_10
201_540
so i should select only the id that finish with '_0'
i tried the below query but it give me all the three ids
select * from A
where id like '%_0'
but i expect only
id|
23_0
have you any idea, please ?
The problem is that _ is a special character. So, one method is:
where id like '$_0' escape '$'
You can also use right():
where right(id, 2) = '_0'
Related
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 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 am in the processing of preparing for migration to a new database system for a stock based retail store. In the current database products which have been deactivated have a leading * added to the record.
The owners do not want to bring deactivated products into the new system so this leading * is my only reference point to work from.
I need to create a SELECT query that will exclude products that have a leading * but so for to no avail.
I have tried the below
SELECT prdcod
FROM prdtbl
WHERE prodcod<>'*%';
The first 10 results returned are:
71A022
051116
070505PRO
*031620
458508
501315
*070247PE
370002
070278STU
*CO20302
I suspect I may not be able to use the * as an excluding factor
Any thoughts would be appreceated
This can be done in so many ways..
Using NOT LIKE
SELECT prdcod
FROM prdtbl
WHERE prodcod NOT LIKE '*%'
Using LEFT/SUBSTRING
SELECT prdcod
FROM prdtbl
WHERE LEFT(prodcod,1) <> '*' -- SUBSTRING(prodcod from 1 for 1) <> '*'
Note : Not Like is preferred approach if you have a Index on prodcod column
try this
SELECT prdcod
FROM prdtbl
WHERE prodcod not like '*%';
SELECT prdcod
FROM prdtbl
WHERE prodcod NOT LIKE '*%';
instead of using "<>" operator, use "not like":
Select prdcod
From prdtbl
Where prodcod not like '*%';
I have ,for example, this table in a Microsoft Access database:
id numeric
context text
numberfield numeric
I want to select every record that ends with 9 in the column"numberfield". This gives a problem because it is a numeric field and as a result I can not use the following SQL:
select * from table where numberfield like "%9"
A solution is that I change the numberfield to a text. But this gives a problem because there are several users and the change might give a problem in the future. Is there an option to select on the ending when it is a number field?
That sound a little fishy.. are you sure you can use that query? Don't know about Access but almost any other DBMS allows it.
If it really doesn't work, you can do this:
select * from table where STR(numberfield) like "*9"
EDIT: Maybe it didn't work because you used % which is used with * in Access :
select * from table where numberfield like "*9"
Numbers are numbers, so use Mod for this:
select * from table where numberfield mod 10 = 9
Instead of casting to string and comparing, just extract the rightmost digit with a MOD operation.
Edit your query as follows:
SELECT *
FROM table
WHERE ((([numberfield] Mod 10)=9));
I'm trying to use a regular expression inside the SAS Data Flux client, which uses Fed SQL. The code is like this:
select * from dataset
where char_value LIKE "ICM[DEF].*"
in order to match all the records where char_value = ICMD... or ICME... or ICMF... .
It doesn't seem to understand the regex, in fact it returns 0 rows.Can you help me?
After looking online for sas synatax, I saw that you are not using the correct syntax.
LIKE operator should be with singlee quotes if you are matching a string, and use % not * so:
select * from dataset
where char_value LIKE 'ICM[DEF]%'
Its untested, so tell me if it works(I'm not familiar with [] so if it doesn't work you can try like 'ICMD.%' or like 'ICME.%'....)
select * from dateset
where char_value like 'ICMD%' or char_value like 'ICME%' or char_value like 'ICMF%'