This should not be that difficult. I thought I had a basic understanding of RegEx, but obviously not.
I simply want want to find all records where the ComputerName field begins with AB or CD.
I am running this in Visual Studio, VB.Net against SQL Server.
I am trying things like: Select ComputerName from TableName where ComputerName like '[AB|CD]%'
I have tried every variation I can think of, but I just can't get it.
Thank you in advance!!
Many different variations using brackets, commas, pipes, carrots, etc.
Use an alternation:
Select ComputerName
from TableName
where ComputerName like '(AB|CD)%'
Or you can just use non-regex like
Select ComputerName
from TableName
where ComputerName like 'AB%'
or ComputerName like 'CD%'
Related
I'm trying to create a request to find all the occurence of a name in my Usernames table:
for example I have:
SAnderso
BBobby
SAnderso1
SAnderso2
SAnderso99
and I'd like to get all the SAnderso(here 1,3,4,5)
here's what I tried:
SELECT * FROM Utilisateur WHERE NomUtilisateur LIKE 'SAnderso%[0123456789]' OR NomUtilisateur = 'SAnderso'
but when I do this nothing is shown in the results
can you help me ?
In any database, this might do want you want:
WHERE NomUtilisateur LIKE 'SAnderso%'
If you specifically need to check for numbers after the name, then the method depends on the database. In most databases, you'll need to use the extension for regular expressions.
In previous questions you have asked about MySQL so, assuming MySQL, you can use REGEXP_LIKE which allows regular expressions to be used:
SELECT * FROM Utilisateur
WHERE REGEXP_LIKE(NomUtilisateur, '^(SAnderso|SAnderso.*[0-9])$')
This is based on your attempt where you used % in LIKE so that there may be characters following "SAnderso" before the digit(s). If you only want "SAnderso" optionally followed by digits then you would change the regex pattern to ^SAnderso[0-9]*$
thanks to #mhawke who helped me find about REGEX in sql database the solution was to use it but in maria db REGEXP_LIKE does'nt exist so I looked on the documentation and foudn out I could do it like this:
SELECT * FROM Utilisateur WHERE NomUtilisateur REGEXP '^(SAnderso|SAnderso.*[0-9])$'
I am a little stuck. I know in normal SQL syntax, you can use NOT LIKE 'x%' to filter anything out that starts with an x.
I am trying to use an HTML SQL code for Fusion Tables, but my NOT LIKE is not working.
I think Fusion Tables does not support NOT LIKE. Any work around to achieve what I want?
I want to display data that does not contain the word 'Development'.
My code is:
https://www.googleapis.com/fusiontables/v1/query?alt=csv&sql=SELECT * FROM My Fusion Table WHERE 'Task Name' IN 'Centre Visits' AND 'Expenditure Organization' NOT LIKE '%25Development%25' &key=KEY
Is 'Task Name' your column? Presuming that and 'Expenditure Organization' are both column names you would want something like this I'm guessing:
SELECT * FROM My Fusion Table
WHERE [Task Name] IN 'Centre Visits'
AND [Expenditure Organization] NOT LIKE '%25Development%25'
Note: your like wildcard is asking for a string not containing '25Development' and then another wild card and then the string ending again with '25'. Perhaps double check to make sure that is what you want.
I am trying to find special characters in any of my fields that are not in the range of a-zA-Z0-9. However if I try this query:
select Name from table where Name like '%[?]%'
I get two records:
???? ?????
Fixed?????
Which is what I want. However, since I don't know what the special chars will be I need to use an exclusion of data that has mixed characters:
select Name from table where Name NOT like '%[a-zA-Z0-9]%'
Since this excludes all records with a-zA-Z0-9 I only get:
???? ?????
But I also need to get the 'Fixed?????' result. I need to get the data that has the special character merged into it.
I am bit at a loss as how to do this. I've seen this done with shell scripts or 'vi' (LIST), but in SQL that's not so easy.
Has anyone out there solved this?
Try this code:
select Name from table where Name like '%[^0-9a-zA-Z ]%'
Thank you for replying. I had tried your suggestions but I was still getting more results. However, it looks like you can get very specific with the exclusion. Eventually I ended up adding results from the data I got.
Like this:
select Name from table where Name LIKE '%[^0-9a-zA-Z() -._/\:=,]%'
This finally gave me what I was looking for. Although new issue I have now is how to suppress the [] brackets which apparently also are found in the data:
???? ?????
HP PCI 10/100Base-TX Core [100BASE-TX,FD,AUTO,TT=1500]
Fixed?????
Adding those brackets into the query breaks the array boundary:
'%[^0-9a-zA-Z() -._/\:=,**[]**]%'
However, this is something I can handle. As long as I am not getting "all" the data.
LIKE '%[^0-9a-zA-Z]%'
numbers (0-9), lowercase alphas (a-z), uppercase alphas (A-Z). The "^" makes that a "NOT" one of these things
I'm modifying code for an ID search bar and I'm trying to enable the user to be able to search for ID's using SQL syntax, so for example '%535%'. Doing just that is simple enough, but I've been searching and racking my brains for a while now and I can't seem to find a solution to the issue described below:
The problem is that the IDs are all left-padded varchar(14), as in:
' 8534'
' 393583'
' 123456/789'
This virtually disables the user from searching only for IDs that begin with a certain sequence, as '85%' returns no results due to the whitespace padding.
The site I'm maintaining is an oldie written in classic ASP (w/ JScript) and the search is done via a stored procedure with the whole 'WHERE' clause being passed in as a parameter.
I'm not able to modify the database, so what I"m asking is: is there any way to modify the clause so that the padding is ignored and '52%' returns IDs beginning with 52?
You want:
where ltrim(id) like '123%'
or, assuming that there are no interior spaces in the id:
where concat(' ', id) like '% 123%'
SELECT LTRIM(ID) FROM table WHERE ID LIKE '%1234%'
Edit as you can only modify WHERE statement
WHERE LTRIM(ID) LIKE '1234%'
Functions in the where clause tend to be slow. Something like this might be quicker:
where id like '123%'
or id like '% 123%'
My query looks for dataset containing a particular label, let say:
SELECT * FROM Authors
WHERE Title LIKE #pattern
where #pattern is defined by user. So, %abc% would match abcd, 0abc, etc. Sometimes there are labels like
Xabc-ONE
blaYabc-TWO-sometext
Zabc-THREE
blubXabc-FOUR
and I'm looking for labels containing abc and ONE or TWO, something like %abc%(ONE|TWO)%. Is it possible?
You can add support for regular expressions to SQL Server via a CLR function, as shown in this answer, but it may not be possible for you in your environment. Check with your friendly sysadmin!
Maybe I don't understand your question right, but why not simply
SELECT * FROM Authors
WHERE Title LIKE '%abc%ONE%' OR '%abc%TWO%'
?
LIKE is just a search with wildcards, nothing more, so there's actually no other way of doing what you want with LIKE. If you need more, have a look into regular expressions. But be aware that it's slower than LIKE and in your case absolutely not necessary.
UPDATE:
From comments "don't want to care for what the user wants to match"...then simply do it like this:
SELECT * FROM Authors
WHERE Title LIKE CONCAT('%', $userInput, '%ONE%') OR CONCAT('%', $userInput, '%TWO%')
Or do I still don't get you right?
If you are using SQL Server you can enable the full text search engine and use the keyboards contains and near to find abc near ONE
I will say in the query(pseudo code)
Select something from table where CONTAINS(column_name, 'abc NEAR ONE')
http://msdn.microsoft.com/en-us/library/ms142568.aspx