I am trying to retrieve a part of comments field in our database, that contains just the e-mail address. So for example, Comment might look like this:
Type of Count: Itemised EPOS: CBE Email Reports to: user@gmail.com
CC user@domain.ie Name of Email recipient: Xxxxx
Store Contact: Person PhoneNumber Wet stock: No Deli: Yes Packaging: Yes Additional Requirements: Payment Terms: On the day.
Supervisor Feedback Count Issues or concerns: No Unusual Storage Areas: No Payment received: Yes Payment sent to Office:
Posted on Monday 29th June 2015 Downtime due to IT issues: None Details:
I have this query:
SELECT st.Comments,
CASE
WHEN CHARINDEX('@',st.Comments) = 0 THEN NULL
ELSE SUBSTRING(st.Comments,beginningOfEmail,endOfEmail-beginningOfEmail)
END email , sc.CustomerName1
FROM tblStockTakes st
INNER JOIN tblCustomers sc ON st.CustomerId = sc.CustomerId
CROSS APPLY (SELECT CHARINDEX(' ',st.Comments + 'Reports to: ',CHARINDEX('@',st.Comments))) AS A(endOfEmail)
CROSS APPLY (SELECT LEN(st.Comments)/2 - CHARINDEX(' ',REVERSE('EPOS: ' + st.Comments),CHARINDEX('@',REVERSE(' ' + st.Comments))) + 2) AS B(beginningOfEmail)
WHERE st.StockTakeDate BETWEEN '2015-06-27 06:00:00.000' AND '2015-10-28 06:00:00.000'
Which returns the following:
Type of Count: Itemised EPOS: CBE Email Reports to: user@gmail.com CC
What should I change in the query to remove the front part, and just start with Email Reports To: xxx@gmail.com?
Thanks for any advise!
This is another way to format your string which might need a little bit of adjustment. I put comments for beginningOfEmail
and endOfEmail
, in case you want to keep your strategy of creating the values in a CROSS APPLY
:
SUBSTRING(st.Comments
,PATINDEX('%Email Reports to: %',st.Comments) --beginningOfEmail
,PATINDEX('%.com CC%',st.Comments)-PATINDEX('%Email Reports to: %',st.Comments)+4) --endOfEmail
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.