Big Query: extract a certain number of signs after a text - extract

my Database has two columns and looks like
Name/ Description
Anna/ Home, TEL:123-45678, Fax:123-45679
Ben/ School, no phone,no FAX
Clair/ Job,TEL:987-65432, Fax:987-45679
Dave/ Home, TEL:147-85236, no FAX
I want eventually to get only those guys who have a phone number and the respective number.
Should eventually look like
Name/ Description
Anna/ TEL:123-45678
Clair/ TEL:987-65432
Dave/ TEL:147-85236
But to now all my tries with left() or right() were no much successful.
like
right(Description, 13)
but this only worked if phone number was the last entry in all the strings.
Also
left(Description, 13)
does not result well, since the phone number is not in the beginning.
Thank you!

The regular expression extraction functions would let you do it, i.e.
select regexp_extract(
'Clair/ Job,TEL:987-65432, Fax:987-45679',
r'TEL:(\d\d\d-\d\d\d\d\d)')
(note that the regexp pattern is prefixed with 'r' to make it a raw string literal)

Related

MySQL db - Can i use a wildcard inside a field?

I'm not sure if this is possible but I have a small MySql database that is used with a call screener app for my PBX.. I can add single numbers such as (555) 123-4567, however I would like to enter in entire blocks of numbers like (555) 123-???? so that any number calling from the numbers (555) 123-0000 through (555) 123-9999 would be selected in one entry. I know you can use wildcards in queries etc., but can they be used inside row or column fields?
I think that should work, if you reverse the parameters of like. So if you have a table with 'number masks' to match with (assuming some table and column names here), it could look like this:
select * from NumberMasks m where :CallerNumber like m.Mask
For clarity: :CallerNumber is the phone number of the caller. NumberMasks is just an assumed name for the table, where Mask would be the column containing the mask to match with in the form of (555) 123-???? as specified in the question.

Capitalize Just the last Letter in string - MS Access

I have a column in my access database table, I ran a query to make it proper case by using StrConv([MyColumn],3) but last two letters are state names and this query makes SOmeThing, soMethINg, NY to Something, Something, Ny,
I want the result as Something, Something, NY
Is there a another query I can run after to capitalize last letter?
You can use:
UcaseLast: Left([YourColumn], Len([YourColumn]) - 1) & UCase(Right([YourColumn], 1))
Well, most people would tell you to store your 'address', 'city', and 'state' as separate fields. Then you Proper Case each separately and concatenate them together. If you can do that... that is your best approach.
If this is a database or file that's been tossed at you and you can't make the field/table changes... it's still possible to get your desired results. However, you better make sure all strings end with your state code. Also make sure you don't have foreign addresses since Canadian (and other countries) use more that two letters for the province code at the end.
But if you are sure all records contain two letter state abbreviations, you can continue with the following:
MyColumnAdj: StrConv(Mid([MyColumn],1,len([MyColumn])-2),3) + StrConv(right([MyColumn],2),1)
This takes the midstring of your [MyColumn] from position 1 to the length of your [MyColumn] minus 2 (leaving off the state code) and it Proper Case's it all.
It then concatenates (using the plus sign) to a rightstring of [MyColumn] for a length of 2 and Upper Case's it.
Once again, this is dangerous if the field doesn't have the State Code consistently at the end of the string.
Best of luck. Hope this helps. :)

Find phone number in longtext field using mysql. return only phone number

I have a site with many pages. I am trying to create a report of all phone numbers in a longtext field type. There is a lot of text in each field, so I don't want all of the content. Just the phone numbers. I am trying to use the area code, 308, to Locate. I would like to start 1 position back and return 14 characters.
This is what I was trying, but I seem to be doing something wrong. Any help would be greatly appreciated.
SELECT SUBSTRING('Fieldname', LOCATE('Fieldname', 308)-1, 14)
FROM

access 2003 text display leading zero

We are currently using Access 2003. I have a vehicle maintenance log which has a text field for vehicle numbers. The field needs to be text as it will have a R in it when a vehicle is retired. They would like to have the field be a four digit number with leading zero's. So vehicle 22 would be displayed in the table and reports as 0022 and when retired it would be R0022. I have tried to change the format of the field to "0000" and "0000"# but neither of these will display the leading zero's.
Do you really want you users to manually edit that field?
I don't like solutions like this, because it's error-prone (unless you check a lot of things to make sure that no one enters invalid data) and feels unelegant to your users.
I would just save the following in the table:
the vehicle number in a numeric field (22, not 0022)
a boolean field which indicates if the vehicle is retired
This is much easier for your users to work with:
they can just enter new vehicle numbers without having to think about leading zeros
to retire a vehicle, they just need to set a checkbox, instead of putting the right letter in front of the vehicle number
Plus, showing the desired number R0022 now becomes just a matter of displaying/formatting the data from the table:
Public Function GetDisplayNo(VehicleNo As Integer, IsRetired As Boolean) As String
GetDisplayNo = IIf(IsRetired, "R", "") & Right("0000" & VehicleNo, 4)
End Function
You can use this function like this:
GetDisplayNo(22, True) returns R0022
GetDisplayNo(22, False) returns 0022
And if you need to display a list of vehicle numbers in a report or a continuous form, you can directly use this function in the underlying query:
SELECT
Vehicles.VehicleNo,
Vehicles.IsRetired,
GetDisplayNo([VehicleNo],[IsRetired]) AS DisplayNumber
FROM Vehicles;

MYSQL — Number formatting issue

I'm working on a gig right now where the client wants the user to be able to search for a product by product code.
A product code is formatted like so: 123.4567.89
So, the search box should return that product whether the user enters the number with the periods, without the periods, or with spaces.
So, all of the following should return the product: 123.4567.89, 123456789, 123 4567 89.
My current query looks like so:
SELECT *
FROM products
WHERE product_code LIKE '%$search_code%'"
I'm at a loss as to how I would revise that to include all the different possibilities of how a user would input these numbers.
Thanks in advance for any help.
[Front End] Limit the characters the user can enter. Only allow periods and spaces. Don't allow any alpha characters (if all your product SKUs are numerical).
[Middle Tier] After the form is posted, double check the data for extraneous characters on the back end. If somehow the client managed to bipass the validation on the front end, you can catch it on the back end. Use a simple search and replace in your language of choice.
[Database/Back-End] Once the data is restricted to only numeric digits and you send the SKU to your database query, strip out all periods on your products table. If you know you only use periods to store the SKUs, just search excluding them, e.g.
SELECT *
FROM products
WHERE REPLACE(product_code,'.','') = #productCode
Avoid wildcard %% searches, they're expensive.
You have to normalize the number input by the user before doing the search, that is: make it have the same format as the number stored in the database.
For instance, if the numbers are stored in the database without the periods (like 123456789), you have to pre-process the number input by the user to also remove the periods, spaces and any other characters from it.
Edit: if the numbers are stored in the database with the periods, than you also need to normalize them by removing the periods as #HertzaHaeon pointed in his answer.
How about removing dots and spaces in both the database code and the searcg input, so you have just the digits? Something like this:
WHERE REPLACE(product_code, '.', '') LIKE '%formatted_search_code%'
For the search input, you can strip everything but digits form it with a regular expression or simple substring replacement.
I think the best solution to your problem is to format the search value so that is matches the format used in your database. But than you will only find the product, if the user fills in the whole product number. If this is not the desired solution and you want to be able to let the user fill in any part of a product code and find al the products that have a code containing that that I think you should filter out the periods in your database.
The fasted solution would be to do it actually in your database. Remove the dots from your product code or add an extra field containing the product code without dots. This will speed up the query when the dataset gets larger.
If you not want to do that you can always filter out the dots in the search query:
REPLACE(product_code,'.','') LIKE '%$search_code%'
This will do the thrick but can be very slow when the dataset gets bigger.
I work with this all the time with social-security numbers. My solution is to take your input string, strip out and characters that are not digits, make sure that the string is the proper length and then use the substring function to break the string up and then put it back together with the delimiters. If you're using PHP, the function might look like this this:
<?php
function FormatProductCode($String) {
$String = preg_replace("/[^0-9]/", "", $String);
if ($String == "") {
return null;
}
$String = str_pad($String, 9, "0", STR_PAD_LEFT);
return substr($String, 0, 3) . "." . substr($String, 3, 4) . "." . substr($String, 7, 2);
}
?>
Use this function any time that you need to input data into the database or compare data.