referencing a specific character out of a string in SQL Server - sql

How do I reference only the second character in a column value with data type varchar(200) so that my SELECT * query will pull out only rows for which the second character is a space?

SQL Wildcard characters are:
%: zero or more characters
_: single character
You query should resemble SELECT * FROM x WHERE column LIKE '_ %'.
LIKE is a string operator that compares each character and suggests that your value should resemble (loosely defined) what follows.
'_ %' means:
Any single character, followed by
The space/white space character
Zero or more of any other character

The operator you are looking for is like. Try this:
select * from table_name where column_name like '_ %';
Here the _ leaves space for any one character, the second character has to be a ' ' and % means anything after that.
Hope this helps.

Related

How to avoid selecting row in sql server which has special symbol

How to avoid selecting row which has special symbol like mentioned below .
We can use range of ASCII character as below. CHAR(n) returns character value of integer ASCII code n
SELECT *
FROM yourTable
WHERE ID NOT LIKE '%['+CHAR(32) +'-'+CHAR(126)+']%'
OR Name NOT LIKE '%['+CHAR(32) +'-'+CHAR(126)+']%';
Refer ASCII characters
You could use SQL Server's enhanced LIKE operator:
SELECT *
FROM yourTable
WHERE ID NOT LIKE '%[^A-Za-z0-9_-]%' AND Name NOT LIKE '%[^A-Za-z0-9_-]%';
This would select only rows where both ID and Name columns do not contain any special characters. Special characters here are defined as anything other alphanumeric, underscore, and hyphen.

Using `_%` together in `LIKE` is not returning exact data

I can guesss it may be easy to answer type question but I am facing it first time, so any help will be more appreciated.
My Query:
SELECT remarks FROM enroll WHERE remarks LIKE 'REC_%'
OUTPUT:
remarks
REC_59161
Reclassify Hedge
Expected Output is only REC_59161. Yes _ is used for matching any single character but I am just looking for achieving my expected output.
_ is a wildcard Character. So that you have to escape it using [].
Query
select remarks
from enroll
where remarks like 'REC[_]%';
The underscore _ character is actually a special character with the LIKE operator, as are %, [] and ^:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql
In that article, you'll see that an underscore _ matches any single character.
Use the ESCAPE keyword to define an escape character to allow you to escape the pattern-matching characters. We can use ! as the escape character, so:
WHERE remarks LIKE 'REC!_%' ESCAPE '!'
_ is a wildcard character, Try this :
declare #enroll table (remarks varchar(50));
insert into #enroll values ('REC_59161') , ('Reclassify Hedge');
SELECT remarks FROM #enroll WHERE remarks LIKE 'REC[_]%';
Demo
There are two issues:
It seems that the column is case insensitive, see Is the LIKE operator case-sensitive with MS SQL server? for details. That's why Rec in Reclassify Hedge fits REC in the Like
_ (as well as %) is a wild card, you should escape _ in the pattern
Query:
SELECT remarks
FROM enroll
WHERE remarks LIKE 'REC*_%' ESCAPE '*' /* SQL 92 standard */
http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
uses the triadic operator LIKE (or the inverse, NOT
LIKE), operating on three character strings and returning a Boolean.
LIKE determines whether or not a character string "matches" a given
"pattern" (also a character string). The characters '%' (percent)
and '_' (underscore) have special meaning when they occur in the
pattern. The optional third argument is a character string
containing exactly one character, known as the "escape character", for
use when a percent or underscore is required in the pattern without
its special meaning.

Using wildcards

Can anyone help me out with this-
There is a column in the database as "TEXT".
This column hold some string value.
I want to search any row that is having '%' in this column .
For eg how will i search for a row having value 'Vivek%123' in the column TEXT
In sql there is something known as an escape character, basically if you use this character it will be ignored and the character right behind it will be used as a literal instead of a wildcard in the case of %
WHERE Text LIKE '%!%%'
ESCAPE '!'
The above sql statement will allow you to search for any string containing a percentage character '%' so it could find anything in the format of
string%string
You must escape the % character
WHERE COL1 LIKE 'Vivek#%123' ESCAPE '#' ;

Where filename like '%_123456_%'

I'm trying to query a table with a like statement. Is _ considered a wildcard symbol in postgres? Would
select * from table where field1 like '%_123_%'
return the same thing as
select * from table where field1 like '%123%'
Here is an example from the official documentation regarding wildcards in Postgres:
'abc' LIKE 'abc' true
'abc' LIKE 'a%' true
'abc' LIKE '_b_' true
'abc' LIKE 'c' false
_ is a wildcard for one character, while % is a wildcard for multiple characters.
Yes, _ is a wildcard symbol that matches one character. It can't be an empty match, so no, those statements are not the same. The first requires the string be at least 5 characters long while the second only requires 3 characters.
If you're familiar with regexes, %123% is equivalent to .*123.*, while %_123_% is equivalent to .+123.+.
From the PostgreSQL manual:
To match a literal underscore or percent sign without matching other characters, the respective character in pattern must be preceded by the escape character. The default escape character is the backslash but a different one can be selected by using the ESCAPE clause. To match the escape character itself, write two escape characters.
yes
_ matches one char while % matches lots of chars.

in ms azure database how do you search for a string containing _

I have this situation on my azure database where I need to search for any rows that contains the _ character. This is a special character on the database so I try to escape it but I get every row as a result.
select * from table where fieldColumn like '%_%'
will return everything on the table
select * from table where fieldColumn like '%\_%'
returns nothing
select * from table where fieldColumn = '_'
works
so how can i get that row that has only one _ and all the other ones that may have the _ on the string?
You can set whatever escape character you want, like this:
select * from table where fieldColumn like '%!_%' ESCAPE '!'
Here I am using the ! as an escape character to tell SQL Server to treat the following character, the _ , as a string literal.
See the documentation for more info: http://technet.microsoft.com/en-us/library/ms179859.aspx
select * from table where fieldColumn like '%_%' escape '\';
LIKE:
escape_character
Is a character that is put in front of a wildcard character to indicate that the wildcard should be interpreted as a regular character and not as a wildcard. escape_character is a character expression that has no default and must evaluate to only one character.