How to extract alphanumeric phrase from a string - if it doesn't exist I wish to flag it - sql

I have a column that can have the following possible values -
ITO26218361281- JANE
SBC28791827135 VATS
SOT21092832917 JOHN DOE
TIM INQ12109283291
JANE DOE 12/15
I only want to extract the 14 characters
alphanumeric phrase from the strings that can look like above. If the record is like (5), I still want that record to exist to be able to call it out as an error. I don't need the exact text to be the same, I just need it to be flagged for error.
Result expected -
ITO26218361281
SBC28791827135
SOT21092832917
INQ12109283291
JANE DOE 12/15 (or flagged as error)

You can use a regular expression to match the pattern you need, 3 letters and 11 numbers.
Using this in WHERE clause, you can match all "valid" values:
SELECT *
FROM TableName
WHERE ColumnName like '%[A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%'
I'm not a master of patterns nor regular expression, so used a "simple to understand" pattern here.
With this query, you extract the data to other table, or UPDATE a table column with the flag you want.
You can see this query working here in sqlfiddle.com

Related

Alteryx Designer - How to retrieve only first and last name from field excluding middle initials?

I need help in writing SQL code in Alteryx Designer.
My table employees contains a column Name with values shown below. However, I need the expected output as shown below.
Please help.
Name:
Smith, Mary K
Koch, J B
Batoon Rene, Anne S
Vaughan-tre Doctor, Maria S
Excepted output:
Smith, Mary
Koch, J
Batoon Rene, Anne
Vaughan-tre, Maria
The middle initials and “Doctor” word is removed.
Not sure why you need to use SQL if you have the data in Alteryx?
So, you need to remove the right hand 2 characters and the word 'Doctor' from each record?
You could use the Formula tool, though I suspect there are numerous other ways:
replace (TrimRight([Name],' '+right([Name],1)),'Doctor','')

SQL WHERE column values into capital letters

Let's say I have the following entries in my database:
Id
Name
12
John Doe
13
Mary anne
13
little joe
14
John doe
In my program I have a string variable that is always capitalized, for example:
myCapString = "JOHN DOE"
Is there a way to retrieve the rows in the table by using a WHERE on the name column with the values capitalized and then matching myCapString?
In this case the query would return two entries, one with id=12, and one with id=14
A solution is NOT to change the actual values in the table.
A general solution in Postgres would be to capitalize the Name column and then do a comparison against an all-caps string literal, e.g.
SELECT *
FROM yourTable
WHERE UPPER(Name) = 'JOHN DOE';
If you need to implement this is Knex, you will need to figure out how to uppercase a column. This might require using a raw query.

Name Correction

Name Correction
As the wedding season is on, John has been given the work of printing guest names on wedding cards. John has written code to print only those names that start with upper-case alphabets and reject those that start with lower-case alphabets or special characters.
Your job is to do the following:
1.Correct the rejected names (names which start with lower case or with a special character). You have to change the first alphabet of the rejected name to Upper case and in the case of special character there will be no change.
2.Output the newly corrected names in ascending order.
Table format
Table: person
Field Type
name varchar(20)
Sample
Sample person table
name
mohit
Kunal
manoj
Raj
tanya
#man
Sample output table
name
#man
Manoj
Mohit
Tanya
Solution Attempted: IN SQL SERVER 2014
select name
from person as per
where (left(per.name,1) like '%[^A-Z]%' or left(per.name,1) like '% %')
union
select Upper(left(per.name,1))+right(per.name,len(per.name)-1)
from person as per
where left(per.name,1)<>left(Upper(per.name),1)
collate Latin1_General_CS_AI
order by per.name
Sample Test Cases Passes,
Still getting wrong answer in some competitor exam.
Please suggest what test case i have not handled.
Since you are only interested in correcting lower case and reporting special characters in the first character position I would use ascii comparision rather than regex.
select name, ascii(left(name,1)),
case
when ascii(left(name,1)) between 97 and 122 then
concat(char(ascii(left(name,1)) - 32),substring(name,2,len(name) -1))
else name
end name
from t
where ascii(left(name,1)) <= 64 or
ascii(left(name,1)) >= 91

Excel 2013 VLOOKUP() based on multiple criteria, retrieving multiple rows

I have an issue that can (I believe) be solved by just excel, and may won't require VBA (I could be wrong). I believe it can be solved by nested functions but the formula I've tried has not worked.
Here's my data:
Name Report # Name
Mark Doe ReportXXX Mark Doe
Connie Doe ReportYYY Connie Doe
Debbie Doe REPORTYYY Debbie Doe
Valerie Doe FSMVALTR1 Valerie Doe
Jeff Doe FSMVALTR1 Jeff Doe
Andy Doe RAZXYBCA1 Andy Doe
Ryan Doe RAZXYBCA1 Ryan Doe
Andy Doe RAZ111111 Jill Doe
Ryan Doe RAZ222112 Amanda Doe
This list goes on for about ~4000 rows in the first NAME and REPORT # columns. In the second NAME column I have ~160 rows.
The second name column identifies all the users who actually use the report, with no duplicates. The two name and report # columns have many duplicates, since users have access to multiple reports, and many of them are the same report used for different purposes. Since the second NAME column has so few rows, the names don't match up all the way through, which can be observed near the bottom of both the NAME columns.
What I need to do is have a VLOOKUP that identifies the name in both of the columns and then returns the report number that each individual has access to across rows (horizontally), not down the columns. It also needs to I.D. numerous reports since individuals have access to anywhere from 1-15 reports, starting at the second and so on after the previous has been extracted.
Ideally it would look something like this:
Name Report # Name ex column ex column ex column
Mark Doe ReportXXX Mark Doe ReportXXX ReportAAA ReportB
I didn't list the other reports "Mark Doe" has access to and these would be somewhere down the long list of ~4000, along with his name repeated multiple times in the first NAME column, but the second "unique" name column would be where it is returning the reports to, across rows.
I made a method that uses additional three columns and it worked for me.
I used the data you provided above.
Add three columns to the left.
in A2 enter =COUNTIFS($D$2:$D2,D2,$E$2:$E2,E2).
in B2 enter =SUMIFS($A$2:$A2,$A$2:$A2,1,$D$2:D2,D2).
in C2 enter =D2&B2.
Now copy those cells to all your 4000 rows.
Now next to the second name column add column headers numbered from 1 to as many reports you think can be the max one user can have (you said 15).
Now enter this vlookup in F2 =IFERROR(VLOOKUP($F2&G$1, your table array starting from column C as absolute/fixed ,3,FALSE),"") in my case it was =IFERROR(VLOOKUP($F2&G$1,$C$2:$E$11,3,FALSE),"").
Copy the formula accross all columns with number headers and down all rows that have names in second name column (160).
your sheet shuld look something like this image
All the best let me know if it worked for you.
Have you explored the option of using a Pivot Table?
The output would look like this:
With the Pivot Table setup like this:
This method would make for a cleaner view, as your current method looks to repeat ~4,000 times (once for each report & name combo). Hopefully this works for what you need.

How do you query only part of the data in the row of a column - Microsoft SQL Server

I have a column called NAME, I have 2000 rows in that column that are filled with people's full names, e.g. ANN SMITH. How do I do a query that will list all the people whose first name is ANN? There are about 20 different names whose first name is ANN but the surname is different.
I tried
and (NAME = 'ANN')
but it returned zero results.
I have to enter the FULL name and (NAME = 'ANN SMITH') ANN SMITH to even get a result .
I just want to list all the people with there first name as ANN
Try in your where clause:
Where Name like 'ANN %'
Should work mate.
ANN% will find all results where ANN is first then anything after.
%ANN% will find the 3 letters ANN in any part of that rows field.
Hope it helps
Also usually Name is separated into First names and second name columns.
this will save Having to use wild cards in your SQL and provide A bit more normalized data.
SELECT NAME
FROM NAMES
WHERE NAME LIKE 'ANN %'
This should wildcard select anything that begins with 'ANN' followed by a space.