Access Database query on finding specific character in a sentence - sql

I'm looking to find specific characters a column that has addresses. So this has numbers, characters and spaces. Using "like" does not seem to work. I tried using "instr," but I can't get it right....Is it because it has spaces?
So for example:
1234 Arlington Hwy
I want to pull up any address records that has "Hwy" in it. Help please!

SELECT * FROM mytable
WHERE column1 LIKE '*Hwy*'
The * operator acts as a wild-card, allowing anything to come before and after "Hwy" to return.

Related

Determining which records to return based on data structure in a field

If I have a field that includes many values like
67495837431978432 ABC1234
and other values like
1234 Something Street
(I know the data shouldn't be like this. It isn't my database. Can't be changed. Please disregard.)
How can I only return records that have data in this field like the former?
I've tried
where SUBSTRING(field, 1, 10) LIKE '%[0-9]%'
with my thought being if the first type of value starts with 17 numbers, and the next type is a street address with many letters in the first 10 characters, I should be able to check if the first 10 characters only include numbers, and return the field based on that, and I should only get back records like
67495837431978432 ABC1234
as I desire to. Not the cleanest, but should work fine for my situation. But that didn't work. I can't see why. Am I making an error I'm not seeing? Is there a better way to do this that is relatively simple?
If you are using SQL Server and you want to check that the first ten characters are digits, you can use like with character classes:
where field LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%'

Regex not working in LIKE condition

I'm currently using Oracle SQL developer and am trying to write a query that will allow me to search for all fields that resemble a certain value but always differ from it.
SELECT last_name FROM employees WHERE last_name LIKE 'Do[^e]%';
So the result that I'm after would be: Give me all last names that start with 'Do' but are not 'Doe'.
I got the square brackets method from a general SQL basics book so I assume any SQL database should be able to run it.
This is my first post and I'd be happy to clarify if my question wasn't clear enough.
In Oracle's LIKE no regular expressions can be used. But you can use REGEXP_LIKE.
SELECT * FROM EMPLOYEES WHERE REGEXP_LIKE (Name, '^Do[^e]');
The ^ at the beginning of the pattern anchors it to the beginning of the compared string. In other words the string must start with the pattern to match. And there is no wildcard needed at the end, as there is no anchor for the end of the string (which would be $). And you seem to already know the meaning of [^e].

Extract digits from character field in SQL

I am moving data from a column in one database to a column in another database using the INSERT INTO command using Squirrel SQL v3.7.
The field I am moving is a character field for telephone numbers that allowed open entries.
The receiving field however should disregard all letters and symbols and only enter in the format ##########
Is there a simple way to do this? The other solutions I've seen have been very involved.
Try this when extracting, or only the translate when inserting the data:
select translate('+ 4854 BBBB cCc 12','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!##$%^&*()-=+/\{}[];:.,<>? ',' ') from yourTable;
I'm not specifically familiar with Squirrel sql, but the easiest way to match non-numeric is with regex. Specifically [^0-9] will match anything in the string that is not a number. I was able to obtain the result described above on my system (ibm iseries) with a:
select regexp_replace(column1,'([^0-9])','') from table1

SQL Server 2005 Update/Delete Substring of a Lengthy Column

I'm not sure if it is possible to do what I'm trying to do, but I thought i would give it a shot anyway. Also, I am fairly new to the SQL Server world and this is my fist post, so I apologize if my wording is poor or if I leave information out. Also, I am working with SQL Server 2005.
Let's say I have a table named "table" and a column named "column" The contents of column is a jumbled mess of characters (ntext data type). These characters were all drawn in from multiple entry fields in a front end application. Now one of those entry fields was for sensitive information that we no longer need and would like to get rid of but I can't just get rid of the whole column because it also contains other valuable information. Most of the solutions I have found so far only deal with columns that have short entries so they are just able to update the whole string, but for mine I think I need to identify the the beginning and the end of the specific substring that I need and replace it or delete it somehow. This is the closest I have gotten to at least selecting the data that I need... AAA and /AAA mark the beginning and the end of the substring that I need.
select
substring (column, charindex ('AAA', column), charindex ('/AAA',column))
from table
where column like '%/AAA%'
The problems I am having with this one are that the substring doesn't stop at /AAA, it just keeps going, and some of the results are just blank so it looks something like:
AAA 12345 /AAA abcdefghijklmnop
AAA 12346 /AAA qrstuvwxyzabcdef
AAA 12347 /AAA abcdefghijklmnop
With the characters in bold being the information I need to get rid of. Also even though row 3 is blank, it still does contain the info that I need but I'm guessing that it isn't returning it because it has a different amount of characters before it (for example, rows 1, 2, and 4 might have 50 characters before them but row 3 might have 100 characters before it), at least that's the only reason that I could think of.
So I suppose the first step would probably be to actually select the right substring, then to either delete it or replace it with a different, meaningless substring like "111111" or something.
If there is more information that you need to be provided with or if I was unclear about anything please let me know and thank you for taking the time to read through (and hopefully answer) my question!
Edit: Another one that gets close to the right results goes something like this
select substring(column,charindex('AAA',column),20) from table
where column like '%/AAA%'
I'm not sure if this approach would work better since the substring i'm looking for is always going to have the same amount of characters. The problem with this one though, is that instead of having blank rows, they are replaced with irrelevant substrings from that column, but all of the other rows do return exactly what I want.
First of all, check your usage of SUBSTRING(). The third argument is for length, not end character, so you would need to alter your query to something like:
select substring (column, charindex ('AAA',column)
, charindex ('/AAA',column)-charindex ('AAA',column))
from table where column like '%/AAA%'
Yes your approach of finding it and then either deleting or replacing it is sound.
If some of the results are blank, it's possible that you are finding and replacing the entire string. If it had not found the correct regular expression in there, you would have not returned the row at all, which is different from returning a black value in that column.

Replace multiple characters with a single line in ORACLE SQL

I have a phone number and zip code field in a table. I am trying to get this information into a common format, and I want to get rid of all the extra junk like dashes, parenthesis, spaces, and letters.
I was wondering if there was a way to do this with the replace function, I tried doing it similarly to how one would in REGEXP_LIKE() and had no luck, this is what I have.
select (REPLACE(numbers.PHONE,'[a-zA-Z._-%() ]',''))
from table numbers;
If there isn't a way to do this that's fine, I just wanted to avoid having to make a whole bunch of replace statements for everything I want to replace.
It would depend on how much junk you have in your zip codes and phones. For example, you could remove all non-digital characters in those fields with a replace like this one:
SELECT REGEXP_REPLACE('234N2&.-#3NDJ23842','[^[:digit:]]+') FROM DUAL
And afterwards you could format the resulting digits with a replace like this:
SELECT REGEXP_REPLACE('2342323842','([[:digit:]]{3})([[:digit:]]{3})([[:digit:]]{4})','\1 \2 \3') FROM DUAL
I know the examples are not valid as zip codes nor phone numbers but I think they might help you.