SAS FedSQL - regular expression - sql

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%'

Related

How to use bigquery to test if string in list of strings with wildcards

I would like to write something equivalent to the below, but it appears not to work. Is this possible in BigQuery?
select *
from table
where lower(field1) in ("%foo%", "%bar%", "%baz%")
I also need this functionality inside a case when clause
case when (lower(field1) in ("%foo%", "%bar%")) then "string1"
else "string2" end as NewField
Use below approach
select *
from table
where regexp_contains(lower(field1), r'foo|bar|baz')

SQL issue with Like operator not working as expected

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'

how to fetch range of values using LIKE operator in oracle?

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');

How to write SQL like pattern

I have a SQL procedure with this SQL:
select CenterExpensesID,sum(NISAmount) NISAmount
from ktrn10_fnCostCentersTransactions(#month_start_date,#todate,'BEI')
where AccountNumber like #like_account
group by CenterExpensesID
#like_account is a parameter which can get a pattern e.g. '18%'
Is it possible to make a pattern which shows all the records "not like '18%'"
the pattern '[^1][^8]%' is not the solution because it excludes also 19,58 etc..
No. If SQL Server supported regular expressions, this would be easy.
One thing you could do is to parse the expression. So, this does what you want:
where (accountNumber like #param and #param not like '^%') or
(accountNumber not like stuff(#param, 1, 1, '') and param like '^%')
You could then pass in the parameter as '18%' for like and '^18%' for not like.
Or, you could just use two parameters:
where accountNumber like #like_param and
accountNumber not like #notlike_param
The default values would be something like '%' for #like_param and '' for #notlike_param.

Regular expressions inside SQL Server

I have stored values in my database that look like 5XXXXXX, where X can be any digit. In other words, I need to match incoming SQL query strings like 5349878.
Does anyone have an idea how to do it?
I have different cases like XXXX7XX for example, so it has to be generic. I don't care about representing the pattern in a different way inside the SQL Server.
I'm working with c# in .NET.
You can write queries like this in SQL Server:
--each [0-9] matches a single digit, this would match 5xx
SELECT * FROM YourTable WHERE SomeField LIKE '5[0-9][0-9]'
stored value in DB is: 5XXXXXX [where x can be any digit]
You don't mention data types - if numeric, you'll likely have to use CAST/CONVERT to change the data type to [n]varchar.
Use:
WHERE CHARINDEX(column, '5') = 1
AND CHARINDEX(column, '.') = 0 --to stop decimals if needed
AND ISNUMERIC(column) = 1
References:
CHARINDEX
ISNUMERIC
i have also different cases like XXXX7XX for example, so it has to be generic.
Use:
WHERE PATINDEX('%7%', column) = 5
AND CHARINDEX(column, '.') = 0 --to stop decimals if needed
AND ISNUMERIC(column) = 1
References:
PATINDEX
Regex Support
SQL Server 2000+ supports regex, but the catch is you have to create the UDF function in CLR before you have the ability. There are numerous articles providing example code if you google them. Once you have that in place, you can use:
5\d{6} for your first example
\d{4}7\d{2} for your second example
For more info on regular expressions, I highly recommend this website.
Try this
select * from mytable
where p1 not like '%[^0-9]%' and substring(p1,1,1)='5'
Of course, you'll need to adjust the substring value, but the rest should work...
In order to match a digit, you can use [0-9].
So you could use 5[0-9][0-9][0-9][0-9][0-9][0-9] and [0-9][0-9][0-9][0-9]7[0-9][0-9][0-9]. I do this a lot for zip codes.
SQL Wildcards are enough for this purpose. Follow this link: http://www.w3schools.com/SQL/sql_wildcards.asp
you need to use a query like this:
select * from mytable where msisdn like '%7%'
or
select * from mytable where msisdn like '56655%'