Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inserting a '.' when encountering a capital letter

Author  Topic 

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2013-06-21 : 13:50:28
Two questions in one.

CREATE TABLE #test (userx NVARCHAR(MAX))

INSERT INTO #test VALUES ('NoodlesNoggin@emailaddress.here')
INSERT INTO #test VALUES ('MoodlesNoggin@emailaddress.here')
INSERT INTO #test VALUES ('SoodlesNoggin@emailaddress.here')
INSERT INTO #test VALUES ('ToodlesNoggin@emailaddress.here')

DECLARE @a NVARCHAR(MAX)


1) How do I get a '.' in the email address? The criteria in English would be "check the string, ignore the first character, find the first occurence of a capital letter and insert a period before it".

2) Having done so, how would I get these records into @a so that I can use SQL Mail to generate an email to them?

Without doing 1), I only seem to be able to get the last value into @a?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-06-21 : 14:19:15
Here is one way:
SELECT STUFF('ToodlesNoggin@emailaddress.here', PATINDEX('%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]%', STUFF('ToodlesNoggin@emailaddress.here' COLLATE Latin1_General_CS_AS, 1, 1, '') ) + 1, 0, '.')
Go to Top of Page

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2013-06-21 : 15:46:47
Outstanding.

Now, courtesy of your code, those four email addresses are correctly formatted; how do I get them into a variable so that I can email them via SQL Mail?
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-21 : 15:57:38
Do you mean like a comma or semi-colon separated list of e-mail addresses?
SET @a = (SELECT userx+';' FROM #test FOR XML PATH(''))
Of course you will need to use Lamprey's stuffing instead of userx in the above to get the period between the first and last names.
Go to Top of Page

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2013-06-22 : 02:19:57
Thanks both, your solutions worked a treat!

(Why do other forums that I've googled insist there was no way to do what I wanted unless I used complicated functions?)

You guys really are SQL wizards.

Go to Top of Page
   

- Advertisement -