how to use the sql character array wilcard? - sql

Hi I am using SQL DB2 and trying to find all values in a column that start with F25 and end with any character that does not contain ..
So for example the following are allowed
F2501
F25AB
F25ab
F25A.
I had imagined I could do something like:
where col like 'F25[!..]'
however this reutrns no rows where I know such codes exist i.e. F25AB.
what am I doing wrong here?

Neither [ nor ! is a wildcard in SQL.
The only wildcards that SQL LIKE supports are % for any number of characters and _ for a single character.
To do what you want, use
where col LIKE 'F25%' -- starts with F25
and col NOT LIKE '%..' -- does not end with ..

Try regular expresión by using xquery. Or if you hace db2 luw 11.1 you can use regular expresión functions directly from SQL.

Related

How to find a row where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL

I need to find rows where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL.
I am doing like below:
SELECT *
FROM test
WHERE Name_test LIKE '%[^A-Za-z _]%'
But It is not working and I also need to exclude any apostrophe.
Kindly help.
If you need to find all rows where column have ONLY numbers and special characters (and you can specify all of required special characters):
SELECT *
FROM test
WHERE regexp_like(Name_test, q['^[0-9'%##]+$]')
as you can see you just need to add your special characters after 0-9.
^ - start
$ - end
About format q'[SOMETHING]' please see TEXT LITERALS here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-1824CBAA-6E16-4921-B2A6-112FB02248DA
If you need to find all rows where column have no alpha-characters:
SELECT *
FROM test
WHERE regexp_like(Name_test, '^[^a-zA-Z]*$');
or
SELECT *
FROM test
WHERE regexp_like(Name_test, '^\W*$');
about \W - please see "Table 8-5 PERL-Influenced Operators in Oracle SQL Regular Expressions" here:
https://docs.oracle.com/database/121/ADFNS/adfns_regexp.htm#ADFNS235
I need to find rows where col have special characters or numbers (except hyphen, apostrophe and space [and presumably single quotes]) in Oracle SQL.
You can use double single quotes to put a single quote in:
WHERE Name_test LIKE '%[^-A-Za-z _'']%'
However, this is not Oracle syntax. If the above works, then I would guess you are using SQL Server. In Oracle:
WHERE REGEXP_LIKE(Name_test, '[^A-Za-z _'']')

SQL : REGEX MATCH - Character followed by numbers inside quotes

I have a column in sql which holds value inside double quotes like "P1234567" , "P1234" etc..
I need to identify only columns which start with letter P and is followed by seven digits (numbers) only. I tried where column like'"P[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"' but it doesn't seem to work.
Can someone please correct me or point me to a thread which can help me out?
Thanks
Standard SQL has no regex support, but most SQL engines have regex extensions added to them on top of the standard SQL. So, for example, if you're using MySQL then you'd do this:
... WHERE column REGEXP '^"P[0-9]{7}"'
And if you're using Postgres then that would be:
... WHERE column ~ '^"P[0-9]{7}"'
(updated to match the double-quote part of the question, I'd misunderstood that to begin with)
How about using length and isnumeric:
Select
*
from
mytable
where
mycolumn like '"P%'
and len(mycolumn) = 10 --2 chars for quotes + 1 for 'P' + 7 for the digits
and isnumeric(substring(mycolumn, 3, 7))=1
This answer is for SQL Server, other DBMS's may have a different syntax for length

How to select all the string characters preceding a . in oracle

I am using Oracle 11 G and have the following set of data:
12.0
4.2
Version.1
7.9
abc.72
I want to return all string characters before the period. What sort of query would I run in order to achieve this? Any help would be greatly appreciated, thanks!
You can try a combination of instr and substr.
Something like this:
select substr(field, 1, instr(field, '.') - 1)
from your_table;
Assuming field always contains a . character on it.
You can also deal with strings without a . by using case, if or any other similar valid conditional function on Oracle's SQL language implementation.
Of course, you can always put this on a function to make it look nicer on your query.

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.

db2 special character 0x1A

How can I seach for a special character like "→" (0x1A)?
An example for my query is:
select * from Data where Name like '%→%'
I want to use instead of "→" something like 0x1A.
I can't use any Java or C# Code. I just have SQuirrel to connect and send commands to the database.
You can use chr() to search the charater:
select * from data where name like '%' + chr(26) + '%'
Old topic, but a current issue nevertheless. My DB2 environment runs on AS400/iSeries and use EBCDIC instead of ASCII. I worked out the character from the EBCDIC table at lookuptables.com.
ASCII 0x1a translates to SUB, and SUB translates to EBCDIC 0x3f.
Using that in SQL looks like this:
select productDescription
from productsTable
where productDescription like '%' || x'3F' || '%'`
Solution 1: Use chr(26).
Solution 2: Write the query in Java or another programming language which lets you build the SQL from pieces and where you can enter hex codes.
This query has worked for me in the past on iSeries DB2.
select * from db/table where posstr(field, x'3F') > 0
Trouble is you have to be certain of the hex value you are searching for in the string. I had a similar situation where the I was sure the hex code for the character was x'3F, but when I sub-string the non-viewable character it was actually x'22. You might want to single out the character that is giving you the issue and see what it's value is.
select hex(substr(field, 21,1)) from db/table where posstr(field, 'StringBeforeCharacter') > 0