MS Access: searching a column for a star/asterisk - sql

I'm looking for a way to search a column of string datatype which contains a * - the problem is that the star or asterisk is a reserved symbol. The following query doesn't work properly:
select * from users where instr(pattern,"*")
How can you write an Access query to search a column for an asterisk?

You can search for reseverd charaters in Access by using square brackets:
select * from users where pattern like "*[*]*"

yay, found it out by myself:
select * from users where instr(pattern,chr(42))

Just use
select * from users where instr(pattern,"*") > 0
From Access: Instr Function
In Access, the Instr function returns
the position of the first occurrence
of a string in another string.

Use the ALIKE function because its wildcard characters do not include * e.g.
SELECT * FROM Users WHERE pattern ALIKE '%*%';
(Edit by DWF: see #onedayone's useful explanation of ALIKE)

Related

How to use SQL "LIKE" operator by using wildcard?

I am currently having trouble finding all rows that contain the following data in a table "%2" or "%20".
For Example: 'Susan rides%2bike across town'.
SELECT * FROM [my_table] WHERE [description] LIKE % %2 %
I know this example will not work, however is there a way to search for these values "%2" or "%20" using the SQL "LIKE" operator? As I am required to find and replace them with a white space.
SELECT * FROM my_table WHERE description LIKE '%\%2%' ESCAPE '\';
For the LIKE keyword you can specify the ESCAPE sequence afterwards. In this example the leading backwards slash escapes the % so it will be interpreted as literal value.
Read more about it in the documentation: https://learn.microsoft.com/en-gb/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15
you can test it in this SQL Fiddle.
Thank you for this.
SELECT * FROM [my_table] WHERE [description] LIKE '%[%]2%'
The solution for the above stated probelem is
select * from my_table where description like'%[%]2%'

Regular Expression Pattern for Search in SQL

I want to search a table which has file name(s) with a {Numerical Pattern String}.PDF.
Example: 1.PDF, 12.PDF, 123.PDF 1234.PDF etc.....
select * from web_pub_subfile where file_name like '[0-9]%[^a-z].pdf'
But above SQL Query is resulting even these kind of files
1801350 Ortho.pdf
699413.processing2.pdf
15-NOE-301.pdf
Could any one help me what I am missing here.
One way to do it is getting the substring before the file extension and checking if it is numeric. This solution only works well if there is only one . character in the file name.
select * from web_pub_subfile
where isnumeric(left(file_name,charindex('.',file_name)-1)) = 1
Note:
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($).
To handle file names with mutliple . characters and if there is always a .filetype extension, use
select * from web_pub_subfile
where isnumeric(left(file_name,len(file_name)-charindex('.',reverse(file_name)))) = 1
and charindex('.',file_name) > 0
Sample demo
As suggested by #Blorgbeard in the comments, to avoid the use of isnumeric, use
select * from web_pub_subfile
where left(file_name,len(file_name)-charindex('.',reverse(file_name))) NOT LIKE '%[^0-9]%'
and len(left(file_name,len(file_name)-charindex('.',reverse(file_name)))) > 0
You can't really do what you are trying to do using plain out of the box sql. The reason you are seeing those results is that the % character matches any character, any number of times. It's not like * in a regex which matches the pervious character 0 or more times.
Your best option would probably be to create some CLR functions that implement regex functionality on the SQL Server side. You can take a look at this link to find a good place to start.
Depending on your version if 2012+, you could use Try_Convert()
select * from web_pub_subfile where Try_Convert(int,replace(file_name,'.pdf',''))>0
Declare #web_pub_subfile table (file_name varchar(100))
Insert Into #web_pub_subfile values
('1801350 Ortho.pdf'),
('699413.processing2.pdf'),
('15-NOE-301.pdf'),
('1.pdf'),
('1234.pdf')
select * from #web_pub_subfile where Try_Convert(int,replace(file_name,'.pdf',''))>0
Returns
file_name
1.pdf
1234.pdf

SQL - search by beginning of a word

I want to write an SQL SERVER statement that searches for the beginning of a word in a string that starts with something.
For example, if I search for 'em' on the Company record, I should get:
Emily, Inc
The Emmmy
NOT
Forget Them
Lemming, LLC
I can do this in PHP by extracting/slicing the string into an array and searching the beginning of each words.
But how I would write this query in SQL server without resorting to Stored procedures/functions?
JW's answer will work for entries where Em is at the very beginning of the field.
If you also need to retrieve values where the word is not the first in the string, try:
SELECT * FROM tableName WHERE CompanyName LIKE 'Em%' or CompanyName LIKE '% Em%'
(This is assuming all word are separated by a space.)
use LIKE
SELECT * FROM tableName WHERE CompanyName LIKE 'Em%'
Another option is CONTAINS, something like:
SELECT ... WHERE CONTAINS(CompanyName, 'Em*');
For MS Access:
SELECT * FROM Table1 WHERE Company_Name LIKE 'Word*'
For more standard DBMSs:
SELECT * FROM Table1 WHERE Company_Name LIKE 'Word%'

Select query that displays Joined words separately, not using a function

I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.

SQLite string contains other string query

How do I do this?
For example, if my column is "cats,dogs,birds" and I want to get any rows where column contains cats?
Using LIKE:
SELECT *
FROM TABLE
WHERE column LIKE '%cats%' --case-insensitive
While LIKE is suitable for this case, a more general purpose solution is to use instr, which doesn't require characters in the search string to be escaped. Note: instr is available starting from Sqlite 3.7.15.
SELECT *
FROM TABLE
WHERE instr(column, 'cats') > 0;
Also, keep in mind that LIKE is case-insensitive, whereas instr is case-sensitive.