I need help in identifying bad data(PERSON_NAME) from below select query due to which query is failing as it is not able to parse FIRST_NAME & LAST_NAME.
SELECT
PERSON_NAME
,Trim(OReplace(PERSON_NAME,StrTok(PERSON_NAME,' ',1),'')) AS AGENT_LAST_NAME
,StrTok(PERSON_NAME,' ',1) AS AGENT_FIRST_NAME
FROM TABLENAME
WHERE CAST(RECORD_START_TS AS DATE) = '2022-11-01';
*** Failure 6706 The string contains an untranslatable character.
Actual Output should look like below:
PERSON_NAME AGENT_LAST_NAME AGENT_FIRST_NAME
Abraham Gomezfitzgerald Gomezfitzgerald Abraham
Adam Tregoning Tregoning Adam
Ajiel Marino Marino Ajiel
Alexander Ford III Ford III Alexander
Fernanda Garvey Hernandez Garvey Hernandez Fernanda
Related
I'm very new to the concept or Regular Expressions and am looking for a wildcard search solution that allows 2 or fewer characters of the string to be wrong/missing/blank, in Snowflake.
For example, if I have a table's column of basketball players' names such as 'lebron james', 'carmelo anthony', 'kobe bryant', below are the results I would like to have matched from another table (consumers' search queries) for 'lebron james':
'lebrn james' (missing 'o')
'lebronjames' (missing a space between fn and ln)
'lebrn jme' (missing 'o' and 'a')
'lebron james' (exact match)
Would anyone be so kind to provide some guidance?
EDITDISTANCE is what you are asking for:
with input(str) as (
select * from values
('lebrn james'), ('lebronjames'), ('lebrn jme')
), targets(str) as (
select * from values
('lebron james'), ('carmelo anthony'), ('kobe bryant')
)
select i.str, t.str, editdistance(i.str, t.str)
from input i
cross join targets t;
gives:
STR
STR_2
EDITDISTANCE(I.STR, T.STR)
lebrn james
lebron james
1
lebrn james
carmelo anthony
14
lebrn james
kobe bryant
10
lebronjames
lebron james
1
lebronjames
carmelo anthony
13
lebronjames
kobe bryant
10
lebrn jme
lebron james
3
lebrn jme
carmelo anthony
13
lebrn jme
kobe bryant
9
I'm trying to do some transformations on a large data set that I'm working on and was hoping for a bit of assistance on a particular grouping. I have a series of records that follow a pattern similar to below:
Language Full Name Customer ID
--------------------------------------
English John Smith 12222
French John Smith 12222
Spanish John Smith 12222
English Karen Wong 55999
Cantonese Karen Wong 55999
I need the data such that the Full Name and Customer ID are not repeated so simply using DISTINCT for that. However, one oddity in the requirement is that all the different languages need to be preserved and squashed into the resulting output so the resulting data needs to look like this:
Languages Spoken Full Name Customer ID
----------------------------------------------------
English, French, Spanish John Smith 12222
English, Cantonese Karen Wong 55999
Sounded like a simple thing but I guess I'm not a big SQL guru and keep getting funny results. Any help would be much appreciated :)
If you're using SQL Server 2017 or Azure SQL than you can just use STRING_AGG
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017
For everything else (covers solutions from SQL Server 2005 and on):
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
Please bear with me, I'm new to Access and SQL.
What I'm trying to do is to write a SQL query to filter through two tables - one contains words that are split into two columns and the other contains text. Essentially, what I want is a new table that gives me all of the exact matches of the two columns of words with the column of text.
Here's an analogous database to simulate what I want as a result:
Table A:
FirstName: LastName:
John Doe
Jane Doe
Josh Smith
James Jones
David Johnson
Table B:
FullName:
Jake Davidson
Mike Peters
Jason James
John Michael Smith
Query Result:
FirstName: LastName: FullName:
John Doe John Michael Smith
Josh Smith John Michael Smith
James Jones Jason James
(notice that the David - Davidson match didn't come up. i.e. I'd like exact matches only)
So help me fill in the blanks:
SELECT TableA.FirstName,TableA.LastName, TableB.FullName
FROM TableA,TableB
WHERE TableB.FullName LIKE (has an exact match with TableA.FirstName--not sure what to put )
UNION
SELECT TableA.FirstName,TableA.LastName, TableB.FullName
FROM TableA,TableB
WHERE TableB.FullName LIKE (has an exact match with TableA.LastName--not sure what to put)
;
This will be dependant on what you want it to do with FullNames with more than two names, like "John Jacob Smith", but, assuming you want it to ignore the middle word[s],
then try
Select firstname, lastname, fullname
from tableA a
Join tableb f
On f.firstname = Mid(a.fullname, 1, InStr(a.fullname, " ")-1)
Join tableb l
On l.lastname = Mid(a.fullname, InStrRev(a.FullNamee, " ")+1)
Here is an approach that compares each FullName to both Firstname and LastName:
select a.Firstname, a.LastName, b.FullName
from tableA as a inner join
tableB as b
on instr(' '&b.FullName&' ', ' '&a.FirstName&' ') > 0 and
instr(' '&b.FullName&' ', ' '&a.Lastname&' ') > 0
It assume that the delimiter for names is a space (as in your example). The comparison attaches a space onto the beginning and end of FullName and then looks for a space-padded first name and last name.
Can I use a table column within a Like operator? I've created an example,
TableA
Names Location
Albert Smith Senior Aberdeen
John Lee London
Michael Rogers Junior Newcastle
Mary Roberts Edinburgh
TableB
Names
Albert Smith
John Lee
Michael Rogers
I want to do a query such as:
SELECT TableA.Location
into NewTable
FROM TableA
WHERE TableA.Names Like '*[TableB.Names]*';
In this case, there would be no match for Mary Roberts, Edinburgh but the first three locations would be returned.
Is it possible to put a column into a like statement?
If not does anyone have any ideas how I could do this?
Hope you can help
PS I can't use an actual asterisk since this is removed and the text italicised, also I have read about using % instead but this has not worked for me.
You can join the two tables and use LIKE within the JOIN clause:
SELECT TableA.Location
into NewTable
FROM TableA
INNER JOIN TableB ON TableA.Names LIKE TableB.Names & '*';
Honestly, I had no idea that you can do this in Access before I tried it just now :-)
I am trying to write a regexp_replace to create a "Friendly" name for some employees. They are currently stored as FIRST <POSSIBLE MIDDLE INITIAL> LAST <POSSIBLE SUFFIX> <MULTIPLE WHITESPACE> SITE_ID
For example,
JOHN SMITH ABC
JOHN Q SMITH ABC
JOHN Q SMITH III ABC
I am trying to write a regex so that I will end up with:
Smith, John
Smith, John Q
Smith III, John Q
The ABC "Site ID" doesn't need to be included in my output.
This is what I tried with little success:
regexp_replace(
employee_name,
'^(\S+)\s(\S+)\s(\S+)',
'\3, \1 \2'
)
Also, I am using Oracle 10G. Any help would be greatly appreciated!
If your names don't show the problems ruakh points out, i.e., there aren't single-letter names or surnames, and no Hispanic names, you can try this regexp:
^(\S+)\s(\S\s)?(\S+)(\s\S+)?\s\s+\S+$
The replacement should be:
\3\4, \1\2