how can i get SQL to take a sting and return the first letter of each word passed into it.
I want to use this UDF for generating initials for peoples names I have in the DB.
names can be 2 (fname, lname)or 3(...mname) words
i am using sql2005
how can i get SQL to take a sting and return the first letter of each word passed into it.
I want to use this UDF for generating initials for peoples names I have in the DB.
names can be 2 (fname, lname)or 3(...mname) words
i am using sql2005
This should work for both "Firstname Lastname" and "Firstname Middlename Lastname" combinations.
Of course, if users have a space in one of their names for some reason you will have an issue parsing this out, but I suspect that would be the case anyways when not storing your names in separate fields.
You can achieve it via xquery as well.
Here is the complete solution.
http://raresql.com/2013/04/12/sql-server-get-the-first-letter-of-each-word-in-a-string-column/
Here's my solution, and it has these features/peculiarities:
I know the OP has specified that there can only be 2 or 3 names in his case. I don't mind. I'm just sharing a solution that works, and if it's not best for the particular problem, it's fine.
So, here's the function:
And here's a test call:
and the result: