I’m trying to make a query on SQL Server 2008 that gets an email suggestion based on the FullName column of the Person table.
Ex: For the Person named Rodrigo David Waltenberg the query should return rodrigo.waltenberg@company.com
But I don’t know how to get the fullname splitted and all lowercase. The fullname may have middlenames as well, but I need only the first and last names. Anyone knows how to do that using SQL?
Leveraging the magic powers of Google oftentimes helps a lot
“MS SQL lowercase string”, 1st hit:
turns a string to lowercase.
“MS SQL replace string”, 1st hit:
replaces whatever you want with the other string you specify in the given string…
So just combine the two, and do it like this:
Lowercasing input, and replacing spaces to dots:
If this is not enough, and only the first and last parts are neededed, leaving out the middle part is a piece of cake again.
Step 1:
“MS SQL index of char in string”, first hit:
Step 2:
“MS SQL leftmost characters of string”, first hit:
Step 3:
“MS SQL rightmost characters of string”, first hit:
Step 4:
“MS SQL reverse string”, first hit:
If it is always true, that names have at least two parts separated by at least one space character, just plug the parts together:
Getting first and last part of input consisting of at least two parts, lowercasing, joining them with dot:
Also, if Fullname column can contain leading and trailing spaces, use a TRIM to get rid of them… That would make it go haywire…
Full solution for any amount of parts of input string:
And please still remember to do research and do try something before posting a question.