Extract digits from character field in SQL - 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

Related

How to translate and what could cause characters such as å¿è€

Goal:
I only use select statements with the dbs I have access to.
One of the columns is supposed to store legible english sentences but there are values with strange characters. I would like to find a way to translate those special characters to legible characters
My question is two fold:
Can I translate the following string to a legible format all stored data is basically lost in translation
How can I ensure that the data is stored correctly?
Column Collation: SQL_Latin1_General_CP1_CI_AS
Column Data Type: NVARCHAR(300)
Data Examples:
å¿è€
ÐžÐ±Ð°Ð¶Ð´Ð°Ð½Ð¸Ñ Ð·Ð°
Use prefix of ‘N’ While you Enter to table
Insert Into TownMessage_Tbl Values (elanat=N' + Elanat +"')

Access Database query on finding specific character in a sentence

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.

Can we select the datas that have spaces between the lines in DB without the spaces?

I have a textbox to make a search in my table.My table name is ADDRESSBOOK and this table holds the personel records like name,surname,phone numbers and etc.The phone numbers holding like "0 123 456789".If I write "0 123 456789" in my textbox in the background this code is working
SELECT * FROM ADDRESSBOOK WHERE phonenumber LIKE "0 123 456789"
My problem is how can I select the same row with writing "0123456789" in the textbox.Sorry for my english
You can use replace():
WHERE REPLACE(phonenumber, ' ', '') LIKE REPLACE('0 123 456789', ' ', '')
If performance is an issue, you can do the following in SQL Server:
alter table t add column phonenumber_nospace as (replace(phonenumber, ' ', '');
create index idx_t_phonenumber_nospace on t(phonenumber_nospace);
Then, remove the spaces in the parameter value before constructing the query, and use:
WHERE phonenumber_nospace = #phonenumber_nospace
This assumes an equality comparison, as in your example.
If there is a specific format in which the Phone number is stored than you can insert space at the specific locations and than pass that to the database query.
For Example as you have mentioned in the question for number 0 123 456789.
If there is a space after first number and space after fourth number then you could take the text from the textbox and insert space at second position and sixth position(as after adding space at second position + next three positions are number so sixth position) and pass that text to the database query.
An important part of Db design is ensuring data consistency. The more consistently it's stored, the easier it is to query. That's why you should make a point of ensuring your columns use the correct data types:
Dates/time columns should use an appropriate date/time type.
Number columns should use a numeric type of the appropriate size. (None of this numeric varchar rubbish.)
String columns should be of the appropriate length (whether char or varchar).
Columns with referential relationships should never store invalid references to the referenced table.
And similarly, you need to determine the exact format you wish to use when storing telephone numbers; and ensure that any time you store a number it's done so consistently.
Some queries will be complex enough as is. As soon as you're unable to rely on a consistent format, your queries to find data need to cater for all the possible variations. They'll be less likely to leverage indexes effectively.
I have seen argument in favour of storing telephone numbers as numeric data. (It is after all a "number".) Though I'm not really convinced because this approach would be unable to represent leading zeroes (which might be desirable).
Conclusion
Whenever you insert/update a telephone number, ensure it's stored in a consistent format. (NOTE: You can be flexible about how the number appears to your users. It's only the stored value that needs to be consistent.)
Whenever you search for a telephone number, convert the search value into the compatible format before searching.
It's up to you exactly where/how you do these conversions. But you might wish to consider CHECK constraints to ensure that if you failed to convert a number appropriately at some point, that it isn't accidentally stored in the incorrect format. E.g.
CONSTRAINT CK_NoSpacesInTelno CHECK (Telephone NOT LIKE '% %')

SQL NVARCHAR(MAX) returning ASCII and Weird Characters instead of Text

I have an SQL Table and I'm trying to return the values as a string.
The values should be city names like Sydney, Melbourne, Port Maquarie etc.
But When I run a select I either get black results or as detailed in the first picture some strange backwards L character. The column is an NVARCHAR(MAX)
SELECT ctGlobalName FROM Crm.Cities
Then I tried using MSSQL's Edit top 200 rows feature and I could see the names of the cities, but also all these weird ascii characters.
Now I didn't create the database, I'm just running queries on it. Some things I've read have suggested it is a problem with the Collation. But the table is SQL_Latin1_General_CP1_CI_AS which matches the server collation.
I'm sure there must be something I can add to my select query to return the values as an ordinary string. Is there something I can do to my select query to return the expected format without the weird characters?
An NVARCHAR datatype can store Unicode characters, which are used for languages that are not supported by the ASCII character set i.e. non-English (or related) languages such as Chinese or Indonesian. If your SQL Server or Windows doesn't have that language installed then you might see strange-looking representations of the data.
On the other hand, it could also be that the application that updates this table has just stored bad data in that column.
Either way you might need to do some string manipulation to strip out the characters you don't want.

Joining tables in a database using differently formated data (MAC addresses)

I have two tables that I'd like to join via the MAC Address field, but each table stores MAC addresses slightly different:
Table 1 data: 0:1e:8:c5:9e:fe
Table 2 data: 00:1e:08:c5:9e:fe
The first one removes starting 0's for ANY of the 6 groups of colon-separated fields.
Is there a way I can join on these in SQL without having to modify the data?
I'm guessing I would have to convert both values to XX:XX:XX:XX:XX:XX, then compare them...I'm just not quite sure how to do that.
You can remove leading zeroes with the REPLACE and STUFF functions:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.ShortMac = STUFF(REPLACE(':' + Table2.LongMac, ':0', ':'), 1, 1, '')
Unfortunately, no, they cannot be joined without modifying the data in some way.
I would avoid using the MAC addresses as primary keys since they can change.
If you must use them, is it possible to ensure they're all being inserted in a consistent format? Once that's taken care of, you could write a script (whether it's in SQL, or using another language in an application that can interface with the db) to update all of your existing records to a consistent format. You can just split them apart at a delimiter like the ':' and code to pad a 0 if needed. This would allow you to do your joins without having to create any sort of conversion methods.
Simple way to convert the values would be to split or tokenize the string on the :. Then loop over the items and if size != 2, prepend a 0, and then concatenate them back together. Kinda brute force, but it'd do the job.
Similar to a brute force approach like #plattitude describes but somewhat simpler you could use a regular expresion and REPLACE where each ':' character matches either ':' or ':0' and the shorter string or '0'+ the shorter string matches the longer.
Sorry in hotel room with no access to build and test expression but someone may like to add it to the answer.