I have the following query
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%'
will that result in answers that have any char+a+\+whatever?
is something like Pa\pe valid as a result?
are Ca% or _a% valid answers maybe?
how does \ behave normally inside an SQL query??
% is a wildcard character that matches zero or more characters in a LIKE clause.
_ is a wildcard character that maches exactly one character in a LIKE clause.
\ is a special character known as an escape character that indicates that the character directly following it should be interpreted literally (useful for single quotes, wildcard characters, etc.).
For example:
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a%'
will select records with txt1 values of 'xa1', 'xa taco', 'ya anything really', etc.
Now let's say you want to actually search for the percent sign. In order to do this you need a special character that indicates % should not be treated as a wildcard. For example:
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%'
will select records with txt1 values of 'ba%' (but nothing else).
Finally, a LIKE clause would typically contain a wildcard (otherwise you could just use = instead of LIKE). So you might see a query containing \%%. Here the first percent sign would be treated as a literal percent sign, but the second would be interpreted as a wildcard. For example:
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%%'
will select records with txt1 values of 'da%something else', 'fa% taco', 'ma% bunch of tacos', etc.
The LIKE clause allows you to find text when you don't know the exact value, such as names beginning with JO would be
LIKE 'JO%'
However, if you are search for something ending with a%, then you need to tell SQL to treat the % as part of what you are searching for. In your example, you are looking for a 3 character string, you don't care what the first letter is, but has to end with a%.
Related
I have an nvarchar(50) column myCol with values like these 16-digit, alphanumeric values, starting with '0':
0b00d60b8d6cfb19, 0b00d60b8d6cfb05, 0b00d60b8d57a2b9
I am trying to delete rows with myCol values that don't match those 3 criteria.
By following this article, I was able to select the records starting with '0'. However, despite the [a-z0-9] part of the regex, it also keeps selecting myCol values containing special characters like 00-d#!b8-d6/f&#b. Below is my select query:
SELECT * from Table
WHERE myCol LIKE '[0][a-z0-9]%' AND LEN(myCol) = 16
How should the expression be changed to select only rows with myCol values that don't contain special characters?
If the value must only contain a-z and digits, and must start with a 0 you could use the following:
SELECT *
FROM (VALUES(N'0b00d60b8d6cfb19'),
(N'0b00d60b8d6cfb05'),
(N'0b00d60b8d57a2b9'),
(N'00-d#!b8-d6/f&#b'))V(myCol)
WHERE V.myCol LIKE '0%' --Checks starts with a 0
AND V.myCol NOT LIKE '%[^0-9A-z]%' --Checks only contains alphanumerical characters
AND LEN(V.myCol) = 16;
The second clause works as the LIKE will match any character that isn't an alphanumerical character. The NOT then (obviously) reverses that, meaning that the expression only resolves to TRUE when the value only contains alphanumerical characters.
Pattern matching in SQL Server is not awesome, and there is currently no real regex support.
The % in your pattern is what is including the special characters you show in your example. The [a-z0-9] is only matching a single character. If your character lengths are 16 and you're only interested in letters and numbers then you can include a pattern for each one:
SELECT *
FROM Table
WHERE myCol LIKE '[0][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]';
Note: you don't need the AND LEN(myCol) = 16 with this.
I have a column that has values that look like the following:
17_data...
18_data...
1801151...data
The data isn't the cleanest in this columns, so I am trying to use a LEFT function to identify the rows that have the 2017 year followed by an underscore LEFT(column, 3) = '17[_]' This doesn't return a single column. So to troubleshoot, I added this WHERE clause to the SELECT statement to see what was getting returned, and I got the value 175 where the actual first three characters are "17_".
Why is this, and how can I structure my WHERE clause to pick up those rows?
When you tried adding 'where' with a rule of LEFT(column, 3) = '17[_]', it was doomed to fail. Operator '=' performs exact comparison: both sides must be equal. That is, it would look for rows whose first 3 characters (left,3) are equal to 17[_], that is, 5 characters, one, seven, bracket, underscore, bracket. Text of 3 characters will not exactly-match 5 characters, ever.
You should have written simply:
WHERE LEFT(column, 3) = '17_'
I guess that you've got the idea for adding a bracket from reading about LIKE patterns. LIKE operator allows you to look for strings contained at start/end/middle of the data.
WHERE column LIKE 'mom%' - starts with mom
WHERE column LIKE '%dad' - ends with dad
and so on. LIKE supports '%' meaning "and then text of any length", and also "_" meaning "and then just one character". This forms a problem: when you want to say "starts with _mom", you cannot write
WHERE column LIKE '_mom%'
because it would also match 9mom, Bmom, and so on, due to _ meaning 'any single character'. That's why in such cases, only in LIKE, you have to write the underscore in brackets:
WHERE column LIKE '[_]mom%' - starts with _mom
Knowing that, it's obvious that you could construct your 'starts with 17_' with LIKE as well:
SELECT column1, column2, ..., columnN
FROM sometable
WHERE column LIKE '17[_]%'
Database I'm using: https://uploadfiles.io/72wph
select acnum, field.fieldnum, title, descrip
from field, interest
where field.fieldnum=interest.fieldnum and trim(ID) like 'B.1._';
What will the output be from the above query?
Does trim(ID) like 'B.1._' mean that it will only select items from B.1._ column?
trim removes spaces at the beginning and end.
"_" would allow representing any character. Hence query select any row that starts with "B.1."
For eg.
'B.1.0'
'B.1.9'
'B.1.A'
'B.1.Z'
etc
Optional Wildcard characters allowed in like are % (percent) and _ (underscore).
A % matches any string with zero or more characters.
An _ matches any single character.
I don't know about the DB you are using but trim usually remove spaces around the argument you give to it.
The ID is trimmed to be sure to compare the ID without any white-space around it.
About your second question, Only the ROWS with an ID like 'B.1.' will be selected.
SQL like
SQL WHERE
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 SQL Server 2008, I need to drop the last two characters from a series of item numbers in our database. not all the numbers are the same format and i only need to drop characters from certain ones. the numbers i need to truncate look like this DOR-12345_X where _X is a revision letter. i tried this
SELECT LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2)
FROM IV00103
WHERE LEFT STR = 'DOR'
but it doesn't like the syntax near FROM or the STR = 'DOR'
Can anyone assist? Do you need more info? I'm really a newb at SQL =) THANKS!
-jon
I think the correct syntax is:
SELECT LEFT(IV00103.VNDITNUM, LEN(IV00103.VNDITNUM)-2)
FROM IV00103
WHERE LEFT(IV00103.VNDITNUM, 3) = 'DOR'
Or:
WHERE IV00103.VNDITNUM like 'DOR%'
It doesn't like the syntax near FROM because you're missing a closing bracket on the LEFT. I suspect what you want is something like this:
SELECT CASE WHEN IV00103.VNDITNUM LIKE 'DOR%' THEN LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2)) ELSE IV00103.VNDITNUM END AS VNDITNUM
FROM IV00103
That selects absolutely everything from the IV00103 table, removing those last two characters only from those that have a value in the VNDITNUM column beginning with DOR.
EDIT: If you want to actually update the contents of the table, you could do it this way using a WHERE:
UPDATE IV00103
SET VNDITNUM = LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2))
WHERE VNDITNUM LIKE 'DOR%'
If you only need the ones that match the pattern DOR-somecharacters_asinglecharacter then you should probably do:
UPDATE IV00103
SET VNDITNUM = LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2))
WHERE VNDITNUM LIKE 'DOR-%\__' ESCAPE '\'
The \_ is an escaped underscore, which will be treated as an actual underscore. The ESCAPE '\' part tells SQL that the \ character is being used to escape special characters in the pattern. The second _ is the special character, and matches a single character.