Sunday, January 20, 2019

Find first name, middle name and last name in excel


How to extract first name, middle name and last name from name
Example 1
1.   Suppose we have names like this below and we want to extract first name and last name out of this list. First of all we need to find out the pattern of this list.  The pattern is that the first name appears after first space or period and the 2nd name is between 2nd and 3rd space and in some cases between 3rd and 4th space till to the end where the first bracket list.
Names
1. Marilyn Monroe (1926 – 1962) American actress, singer, model
2. Abraham Lincoln (1809 – 1865) US President during American civil war
3. Mother Teresa (1910 – 1997) Macedonian Catholic missionary nun
4. John F. Kennedy (1917 – 1963) US President 1961 – 1963
5. Martin Luther King (1929 – 1968)  American civil rights campaigner
6. Nelson Mandela (1918 – 2013)  South African President anti-apartheid campaigner
7. Queen Elizabeth II (1926 – ) British monarch since 1954
8. Winston Churchill (1874 – 1965) British Prime Minister during WWII
9. Donald Trump (1946 – ) Businessman, US President.
10. Bill Gates (1955 – ) American businessman, founder of Microsoft
11. Muhammad Ali (1942 – 2016) American Boxer and civil rights campaigner
12. Mahatma Gandhi (1869 – 1948) Leader of Indian independence movement
13. Margaret Thatcher (1925 – 2013) British Prime Minister 1979 – 1990
14. Christopher Columbus (1451 – 1506) Italian explorer
15. Charles Darwin (1809 – 1882) British scientist, theory of evolution
16. Elvis Presley (1935 – 1977) American musician
17. Albert Einstein (1879 – 1955) German scientist, theory of relativity
18. Paul McCartney (1942 – ) British musician, member of Beatles

Extracting First Name

v  To find the first period or space, we will use MID() FUNCTION along with search() or Find() function

v  =mid(text,start_num,num_char)
i)             text = A2 (as below)
ii)           start_num = char position after 1st space
=SEARCH(" ",A2,1) + 1 (result is 3 in the above case)     
iii)          num_char = length of the char to extract. In this case, it will be after start_num till 2nd space position.
a)   First find the 2nd space char position
=SEARCH(" ",A2,SEARCH(" ",A2,1)+1) (result = 11)
In place 1 (as in ii) in the same function, paste the same search function by adding 1.
b)   Deduct 1st space char position from 2nd space char position and reduce it by 1
v  Now paste the above in the mid() function
=MID(A2,SEARCH(" ",A2,1)+1,SEARCH(" ",A2,SEARCH(" ",A2,1)+1)-SEARCH(" ",A2,1)-1)

Extracting Last Name when there is no middle name


=mid(text,start_num,num_char)
v  First find the start_num in mid() function
start_num would be the position after the 2nd space position
=SEARCH(" ",A2,SEARCH(" ",A2,1)+1) +1
v  num_char (length of char)
a)           First find the 1st bracket position [“(“]
=SEARCH("(",A2,1)
b)           Deduct 2 from above to get the position of last char of last name
c)            Get the no. of chars for
num_char = (Bracket positon -2) – (2nd space position+1)
num_char = (SEARCH("(",A2,1)-2 - SEARCH(" ",A2,SEARCH(" ",A2,1)+1))

v  Now create the final formula to the get the last name
=MID(A2,SEARCH(" ",A2,SEARCH(" ",A2,1)+1)+1,SEARCH("(",A2,1)-2-SEARCH(" ",A2,SEARCH(" ",A2,1)+1))

Extracting Last Name when there is middle name

=mid(text,start_num,num_char)

v  First find the start_num in mid() function
start_num would be the position after the 3rd space position
=SEARCH(" ",A6,SEARCH(" ",A6,SEARCH(" ",A6,1)+1)+1)
In order create this function, use the following technique:
·         =SEARCH(" ",A6,1)
·         =SEARCH(" ",A6, SEARCH(" ",A6,1)+1) [put the same function in place of 1 (start_num) and add 1]
·         =SEARCH(" ",A6,SEARCH(" ",A6,SEARCH(" ",A6,1)+1)+1) [put the same function in place of 1 (start_num) and add 1]
v  num_char (length of char)
a)           First find the position of 1st bracket position [“(“]
=SEARCH("(",A6,1)
b)           Deduct 2 from above to get the position of last char of last name
=SEARCH("(",A6,1)-2
c)            Find the position of 1st char of last name just after the 3rd space position
=SEARCH(" ",A6,SEARCH(" ",A6,SEARCH(" ",A6,1)+1)+1)
d)           Get the no. of chars for
num_char = (b - c)
num_char = (SEARCH("(",A6,1)-2 - =SEARCH(" ",A6,SEARCH(" ",A6,SEARCH(" ",A6,1)+1)+1)

v  Now create the final formula to the get the last name
= =MID(A6,SEARCH(" ",A6,SEARCH(" ",A6,SEARCH(" ",A6,1)+1)+1)+1,SEARCH("(",A6,1)-2-SEARCH(" ",A6,SEARCH(" ",A6,SEARCH(" ",A6,1)+1)+1))






 [A1]

Tuesday, January 1, 2019

Tableau with Sql Server Example 1


1. Suppose there are 10 types of defects allocated to different products and the database is built like this example below. Product 12244 has defect type 5,6,8,9 and 10 etc..


2. If we plot this data directly in tableau, generating dashboards will be a bit difficult. In order to visualize the data in tableau easily, we need to arrange this data through a custom query.
3. Write the query in SSMS like below:
SELECT 
ProductID, 
Product_Name,
'Defect_Type1' as Type,
Defect_Type1 as Defect_Desc
from dbo.Defects

4. This will pull all the records of the first defect "Defect_Type1". Similarly, copy this code above and paste after this code with Union All and change the 2nd defect to "Defect_Type2", see below:

SELECT 
ProductID, 
Product_Name,
'Defect_Type1' as Type,
Defect_Type1 as Defect_Desc
from dbo.Defects
UNION ALL
SELECT 
ProductID, 
Product_Name,
'Defect_Type2' as Type,
Defect_Type2 as Defect_Desc
from dbo.Defects

5. Continue the same with other defects:

SELECT ProductID, Product_Name,'Defect_Type1' as Type, Defect_Type1 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type2 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type3 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type4 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type5 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type6 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type7 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type8 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type9 as Defect_Desc from dbo.Defects
UNION ALL
SELECT ProductID, Product_Name,'Defect_Type2' as Type, Defect_Type10 as Defect_Desc from dbo.Defects

6. This above code need to be used in Tableau
7. Open Tableau and connect the SQL Server database instance
8. Click on "New Custom SQL" under Data Source.


9. A dialog box will appear. Paste the SQL query, as below and click Ok:
10. It will look like this
11. Now prepare your dashboard as per your requirement.