I need to run a query that would pull information from a field that has 2 types of data .
Field is address and has 123 avenue as data and bb#yahoo.com.
I need to make 2 fields one for email and one STaddress from table customer and field address?
anyone can i assis..
its access and vb query
I thought of this
Select customer.address from customer where address like "#"
but still i need to display the the data of address field to 2 different fields...
Here is the query that fetches the two different fields:
select iif(field like '*#*', field, NULL) as email,
iif(field like '*#*, NULL, field) as address
from t
The usage of like in Access is a bit different from other databases.
I would suggest that you create a view with this logic. If you actually want to modify the table, you will have to add columns and populate them with logic like the above.
Based on this question and your duplicate question, I understand your table has a field which includes both the street address and email address and you want to split those into separate fields.
So your table includes this ...
YourField
------------------------------
1234 ave willie haha#yahoo.com
123 avenue bb#yahoo.com
And you want this ...
YourField street_address email_address
------------------------------ --------------- --------------
1234 ave willie haha#yahoo.com 1234 ave willie haha#yahoo.com
123 avenue bb#yahoo.com 123 avenue bb#yahoo.com
If that is correct, you can use the InstrRev() function to determine the position of the last space in YourField. Everything before the last space is the street address; everything after is the email address.
SELECT
y.YourField,
Left(y.YourField, InstrRev(y.YourField, ' ') -1) AS street_address,
Mid(y.YourField, InstrRev(y.YourField, ' ') +1) AS email_address
FROM YourTable AS y;
You may need to add a WHERE clause to ensure the query only tries to evaluate rows which include your expected YourField value patterns.
Try something like this:
select *,
(case locate('#', address) when 0 then null else address) as email,
(case locate('#', address) when 0 then address else null) as street
from table;
You'd probably need to adjust the name of "locate" function - I'm not sure if it is the same in access database.
If you have a street address and then an email address in the same field, and want to split them, then use this.
We'll call your original field Addy, and the new fields Street & Email.
SELECT Left(Addy, InStrRev(Addy, " ", InStr(Addy, "#")) - 1) AS Street, mid(Addy, InStrRev(Addy, " ", InStr(Addy, "#")) + 1) AS Email.
What this does is look for the #, then go backwards and look for the first space. The first function takes every left of that space and calls it Street, while the second function takes everything to the right.
The function is really 3 functions nested together. To really understand what it's doing, I'll dissect it.
I = InStr(Addy, "#")
J = InStrRev(Addy, " ", I)
Street = Left(Addy,J-1)
Related
I was given a patient list with names and I am trying to match with a list already in our database and am having troubles given the format of the name field in the patient list. This list is taken from a web form so people can input names however they want so it does not match up well.
WEBFORM_NAME
PATIENT_NAME
JOHN SMITH
SMITH,JOHN L
SHANNON BROWN
BROWN,SHANNON MARIE
Is there a way to use a LIKE clause in an instance like this? All I really need is the LIKE clause to find the first name because I have joined on phone number and email address already. My issue is when households have the same phone number and email address (spouses for example) I just want to return the right person in the household.
Not sure if all you need is to get first name, here is the WIldCard expression to get first name
SELECT LEFT(WEBFORM_NAME,CHARINDEX(' ',WEBFORM_NAME)-1) AS FirstName1,
SUBSTRING(PATIENT_NAME,CHARINDEX(',',PATIENT_NAME)+1,(CHARINDEX(' ',PATIENT_NAME)-CHARINDEX(',',PATIENT_NAME))) AS FirstName2
FROM yourTable
The assumption here seems to be that the webform (where user would manually) type in the name would be of the format <First Name> [<optional middle Name(s)>] <Last Name>, where as the data stored in the table are of the form <Last Name>,<First Name> [<optional middle Name(s)>]. Its not an exact science, but since other criteria (like email, phone etc) have been matched best case
select *
from webform w, patient p
where
-- extract just the last name and match that
regexp_like(p.name,
'^' ||
regexp_extract(w.name,
'([^[:space:],][[:space:],])*([^[:space:],]+)', 1, 2))
and -- extract the first name and match it
regexp_like(p.name,
',[[:space:]]*' ||
regexp_extract(w.name, '(^[^[:space:],]+)'))
Since webform is free form user input, its hard to handle abbreviated middle name(s) and other variations so using the above will do first name and last name based matching which in addition to the matching you are already doing should help.
I am working in SQL Server 2014. I have 2 columns in a table named Gallery Name and Address. Below is my sample data from this table.
Gallery Name Address
--------------------------------------------------------------
Boltelang Boltelang street 12, Paris
BERG Contemporary BERG Contemporary gorge ave, New York
Like this, I have a total of 207 records. Now the issue is that the data of column Gallery Name is being repeated in address column which I want to remove from address column. So it should be like this in
Gallery Name Adress
--------------------------------------------
Bltelang street 12, Paris
BERG Contemporary gorge ave, New York
I have no clue which function I can use in SQL Server to achieve this
Answer to your query is to use REPLACE like following.
UPDATE TABLE_NAME
SET [Address] = REPLACE([Address], [Gallery Name],'')
Note: In above query, REPLACE will replace all the occurrences of [Gallery Name] in [Address].
There can be scenario where you need to replace only the first occurance, not if it exists somewhere in the middle. In that case you can try like following.
UPDATE T SET [Address]=
LTRIM(STUFF([Address], CHARINDEX([Gallery Name], [Address]), LEN([Gallery Name]), ''))
from TABLE_NAME T
WHERE [Address] LIKE [Gallery Name] +'%'
I would go for:
update t
set address = stuff(address, 1, len(gallery_name) + 1, '')
where address like gallery_name + ' %';
These cover the examples in your question, where the gallery name is at the beginning of the address followed by a space. I am very conservative in making such changes. If you have another pattern, then you can run another update to fix those.
You want to be careful using REPLACE(), in case the address repeats the gallery name:
New York New York 10 New York Ave
Assuming it's only needed when the "Gallery Name" is at the start of the "Address".
To correct the addresses in the table
UPDATE [YourTable]
SET [Address] = LTRIM(REPLACE([Address], [Gallery Name],''))
WHERE [Address] LIKE CONCAT([Gallery Name],'%')
Using SQL Server 2014 and the following data:
ID Address City State
1 55 Main St Dallas TX
2 4 Elm Blvd Cupertino CA
3 66 Walnut Miami FL
4 21 Main Ave Cupertino CA
I'm trying to use a contains query across multiple columns to find matches, but can't figure out the proper syntax. In this case, I have the query part:
CONTAINS ((Address, City, State), '"main" or "cupertino")
This returns rows #1, #2, & #4.
I can also try this:
CONTAINS ((Address, City, State), '"main" and "cupertino")
This doesn't return any rows.
What I'm trying to figure out though, is how I would return just row #4 using the search terms "main" and "cupertino" using a contains query.
So basically, I'm trying to do the following but with a contains query:
WHERE (Address LIKE '%main%' OR City LIKE '%main%' OR Zip LIKE '%main%') AND (Address LIKE '%cupertino%' OR City LIKE '%cupertino%' OR Zip LIKE '%cupertino%')
Thanks!
The expression is for CONTAINS() is computed on each each column independently (as you might have guessed). One solution is to try:
CONTAINS((Address, City, State), '"main"') AND
CONTAINS((Address, City, State), '"cupertino"')
The more conventional method is to add a computed column and use that for the indexing:
alter table t add acs as (Address + ' ' + City + ' ' + State) persisted;
Then build the index on that column.
I am using PL/SQL v7.1
I am trying to find all address records where the country name has been entered in one of the address line fields, and also the country field.
The problem is that the country details have not been entered consistently eg.
addr4 addr5 country
---------- ---------- ---------------
JERSEY UK(JERSEY)
IRELAND REPUBLIC OFIRELAND
DOUGLAS ISLE OF MAN UK(ISLE OF MAN)
So, I need to find the records where ANY PART of the Country field is also found in either addr4 or addr5.
I started with this
SELECT *
FROM test_addresses
WHERE addr4||addr5 LIKE '%'||country||'%'
I know this doesn't work because it will, taking the 1st record as an example, check if 'UK(JERESEY)' is found in addr4||addr5 and ,so, no match will be found. But how do I make it check if 'JERSEY' is found in addr4||addr5
Try this way:
SELECT *
FROM test_addresses
WHERE (addr4 is not null and country like '%'||addr4||'%')
or (addr5 is not null and country like '%'||addr5||'%')
Sql Fiddle Demo
I don't know so much about plsql
but I think your query is backwards, try this.
SELECT *
FROM test_addresses
WHERE country LIKE '%'||addr4||'%'
or country LIKE '%'||addr5||'%'
I am using Double-Metaphone for fuzzy searching within my database. I have a table of names, and both the first and last names have double metaphone entries already created (and updated, via a Trigger). In my application, I am allowing the user to search by Lastname and/or Firstname.
What is the best way to query the database, to get the best results from the Double-Metaphone indexes when dealing with both last AND first names ? Querying just based on lastname is easy - generate the DM tags and query the database. It's when querying by both first and last that I'd like to get some fine tuning.
The database layout is similar to the following:
tblName
FirstName
LastName
MetaPhoneFN1
MetaPhoneFN2
MetaPhoneLN1
MetaPhoneLN2
Application: [Lastname] [FirstName]
User inputs just a lastname, or a combination of lastname + [First initial, first name, part of first name].
Lastname: SMITH
FirstName: J or Jo or John or Johnathan
If I pass in "J" as the firstname - I'd like all name entries matching "J%".
If I pass in "JO" as the firstname - I'd like all name entries matching "JO%".
If I pass in "JOHN" or "JOHNATHAN" as the firstname - I'd like to use DM
or maybe also "JOHN%" ?
I'm really open to suggestions here, for the firstname. I want the results to be as good as possible and return what the user wants.
What is the best way to query the database for last + any of those combinations of first name ? Here's a sample of what I've gotten so far.. and I'm not completely thrilled with the results:
SELECT *
FROM tblName
WHERE
--There will always be a last name
(MetaPhoneLN1 = #paramMetaPhoneLN1
OR (CASE WHEN #paramMetaPhoneLN2 IS NOT NULL AND MetaPhoneLN2 = #paramMetaPhoneLN2 THEN 1
WHEN #paramMetaPhoneLN2 IS NULL THEN 0
END) = 1)
-- Match Firstname 1
AND (CASE WHEN #paramMetaPhoneFN1 IS NULL THEN 1
WHEN #paramMetaPhoneFN1 IS NOT NULL AND MetaPhoneFN1 = #paramMetaPhoneFN1 THEN 1
WHEN LEN(#paramMetaPhoneFN1) > 1 AND LEN(#paramMetaPhoneFN1) < 4 AND MetaPhoneFN1 LIKE #paramMetaPhoneFN1 + '%' THEN 1
WHEN LEN(#paramMetaPhoneFN1) = 1 THEN 1
END) = 1
-- Match Firstname 2
AND (CASE WHEN #paramMetaPhoneFN2 IS NULL THEN 1
WHEN #paramMetaPhoneFN2 IS NOT NULL AND MetaPhoneFN2 = #paramMetaPhoneFN2 THEN 1
WHEN LEN(#paramMetaPhoneFN2) > 1 AND LEN(#paramMetaPhoneFN2) < 4 AND MetaPhoneFN2 LIKE #paramMetaPhoneFN2 + '%' THEN 1
WHEN LEN(#paramMetaPhoneFN2) = 1 THEN 1
--ELSE 0
END) = 1
AND (CASE WHEN #paramFirstName IS NULL THEN 1
WHEN FirstName LIKE #paramFirstName + '%' THEN 1
--WHEN LEN(#paramMetaPhoneFN1) = 1 AND #paramFirstName IS NOT NULL AND LEN(#paramFirstName) > 1 AND FirstName LIKE #paramFirstName + '%' THEN 1
--ELSE 1
END) = 1
What I've tried to do is account for the different variations for firstname. My results however, aren't exactly what I would want.
I've been able to find lots of implementations of Double Metaphone in SQL/C#, etc. for /generating/ the Double-Metaphone values, but nothing on how to actually query the database effectively once you have those values.
SUMMARY:
When I search by both lastname and firstname -- I'd like to query the database for the Double Metaphone match only on Lastname, but I'd like a lot of flexibility when a firstname is also passed in.. first initial ? sounds like ? etc. I am open to suggestions and SQL examples!
UPDATE 1:
When I say that I'm not thrilled with the results.. what I'm saying is that I'm not sure how to formulate the Firstname part of the query, to maximize results. If I search for "WILL" - what results should be returned ? WILLIAM, WILL, WILBERT .. but not WALKER - though with what I have here, WALKER would be returned because WILL -> FL and WALKER IS [FLKR] but WILLIAM IS [FLM]. If I do only DM = DM then I wouldn't get WILLIAM even returned, which is why I'm doing a LIKE in the first place, if the DM length is < 4.
Basically, I'd like to know if anyone else has run into this issue, and see what solutions others have come up with.
First initial only - should show all firstnames starting with that initial
- Here's where I'm uncertain:
Partial name - should should all firstnames starting with the partial ? [how do you know if it's just a partial name ?!]
Full name - should use DM ?
It's up to you to decide your business rules on what to return, and what to consider using LIKE vs. DM (or both) on.
Once thing you seem to not considering, though is length of the DM value.
If I search for "WILL" - what results should be returned ? WILLIAM,
WILL, WILBERT .. but not WALKER - though with what I have here, WALKER
would be returned because WILL -> FL and WALKER IS [FLKR] but WILLIAM
IS [FLM]. If I do only DM = DM then I wouldn't get WILLIAM even
returned, which is why I'm doing a LIKE in the first place, if the DM
length is < 4.
So, for this case:
WILL -> FL and WALKER IS [FLKR] but WILLIAM > IS [FLM]
Assuming you are OK with returning multiple matches with best match at top, you would order the results by the length of the stored matching DM value ascending. So, WALKER would be suggested before WILLIAM.
For the first names, again assuming you are OK with returning multiple possible matchs, you could return exact string matches first (non-DM), followed by exact DM matches, followed by partial DM and LIKE matches ordered by the shortest DM matches first, and then LIKE matches and then the rest of the longer DM matches. This is often easiest done with a bunch of UNIONed queries.
You could also choose to rank the LIKE matches by how much the returned string length differs from the input string length (smaller difference = better match).
The difficulty you are facing is that you are combining searching abbreviated names with phonetically similar names. Those two aims are sometimes opposing each other.
Just to throw you another complication, ;-), Bill is also an abbreviation of William.
My thoughts on this subject are that it's probably best to treat names that could be abbreviated or are abbreviations as a separate issue from the phonetic matching. Once you come up with a solution for the abbreviations, then feed the results through metaphone.