How do include space in like statement SQL - sql

I am wondering how to query any number of characters then a space then any number of characters then a comma?
I am looking for last names in a table that contains a space before the comma. For example, they query would return SMITH III, Richard.
Thanks in advance

The % symbol is used to denote a wildcard, you can use it like this:
SELECT * FROM [myTable] WHERE [myField] LIKE '% %,%'

In SQL Server you may find performance better with PATINDEX.
SELECT * FROM [myTable] WHERE PATINDEX('% %,%',[myField]) > 0

Related

SQLite select rows containing whole word

I need to select rows that contains a whole word, NOT a substring. For example from these rows:
System works good
I have some goodies
I need to get only first one (that contains word good), but with my sql query I get both of them.
select *
from my_tab
where my_column like '%good%'
Please note, I need this in sqlite, where not all usual sql functions are available.
You can check it like a space between both % symbols and a and % int end of the word and a % and to the beginning of the word which you need to search.
Query
select * from your_table_name
where words like '% good %'
or words like 'good %'
or words like '% good';
Try
select *
from table
where ' '||words||' ' like '% good %'

How to select rows with Chinese-Japanese characters?

How can I filter a column by Chinese-Japanese like characters? I am trying to do something like
SELECT * FROM my_table WHERE column LIKE '%[A-Za-z]%'
Is it possible for Chinese or Japanese characters?
When working with unicode string you will always need to prefix your string with N to tell sql server explicitly that there can be unicode characters in the operation. INSERT, UPDATE, SELECT and DELETE its true for all operations.
In your case when selecting data, in where clause you will need to prefix the Search string with N. Something like this....
SELECT *
FROM my_table
WHERE column LIKE N'%[A-Z]%' --<-- using Japanese characters here
OR Column LIKE N'%[a-z]%' --<-- using Japanese characters here
Below may work as it did for me.
SELECT * FROM my_table WHERE LEN(RTRIM(my_column)) <> DATALENGTH(RTRIM(my_column))
The len function may ignore trailing whitespaces so it's best to trim it before measuring the length.
Above came from advise on a Japanese web page.

LIKE contains %

I have record in table like 'abc 100% text'.
I want to search all the records that contain 100%.
What will be LIKE query?
SELECT * FROM TABLE where ColName LIKE '100%%'
above query returns wrong results.
Thanks.
SELECT * FROM TABLE where ColName LIKE '%100[%]%'
Have a look at Using Wildcard Characters As Literals
You can use the wildcard pattern matching characters as literal characters. To use a wildcard character as a literal character, enclose the wildcard character in brackets.
SELECT columns FROM table WHERE column LIKE '%[%]%'
or
SELECT columns FROM table WHERE column LIKE '%\%%' ESCAPE '\'
as described in http://web.archive.org/web/20150519072547/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-search-for-special-characters-e-g-in-sql-server.html
Additionally, you can use escape charaters...
LIKE '%100^%%' ESCAPE '^'
http://web.archive.org/web/20150519072547/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-search-for-special-characters-e-g-in-sql-server.html
Use a backslash to escape:
SELECT * FROM TABLE where ColName LIKE '%100\%%';
You may need to try this:
SELECT * FROM TABLE where ColName LIKE '%100\%%' ESCAPE '\';

Use like in T-SQl to search for words separated by an unknown number of spaces

I have this query:
select * from table where column like '%firstword[something]secondword[something]thirdword%'
What do I replace [something] with to match an unknown number of spaces?
Edited to add: % will not work as it matches any character, not just spaces.
Perhaps somewhat optimistically assuming "unknown number" includes zero.
select *
from table where
REPLACE(column_name,' ','') like '%firstwordsecondwordthirdword%'
The following may help: http://blogs.msdn.com/b/sqlclr/archive/2005/06/29/regex.aspx
as it describes using regular expressions in SQL queries in SQL Server 2005
I would definitely suggest cleaning the input data instead, but this example may work when you call it as a function from the SELECT statement. Note that this will potentially be very expensive.
http://www.bigresource.com/MS_SQL-Replacing-multiple-spaces-with-a-single-space-9llmmF81.html

How can I search for numbers in a varchar column

I've got a simple nvarchar(25) column in an SQL database table. Most of the time, this field should contain alphanumeric text. However, due to operator error, there are many instances where it contains only a number. Can I do a simple search in SQL to identify these cases? That is, determine which rows in the table contain only digits in this column. As an extension, could I also search for those column values which contain only digits and a space and/or slash.
In other languages (eg. Perl, Java) a regular expression would resolve this quickly and easily. But I haven't been able to find the equivalent in SQL.
yes you can achive this by using isnumeric function available in sql sever
check more at this link : http://msdn.microsoft.com/en-us/library/aa933213(SQL.80).aspx
All the answers referred to the isnumeric function, but they are not correct, I've mentioned in a comment for all the answers the flaw in the answers. The correct solution is to use a regular expression in your where clause, which contains not like '%[^0-9]%', see the example below:
select column_name from table_name where column_name not like '%[^0-9]%'
select column_name from table_name where IsNumeric(column_name) <> 1
Numeric Only:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1
With Space:
SELECT * FROM Table WHERE Field LIKE '% %'
With Slash:
SELECT * FROM Table WHERE Field LIKE '%/%'
Combined:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1 OR Field LIKE '% %' OR Field LIKE '%/%'