Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

I need help with my query. I found part of this, but doesn't work. Please help. Expand / Collapse
Author
Message
Posted Wednesday, January 2, 2013 6:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, January 7, 2013 5:30 PM
Points: 16, Visits: 28
I need to get the emails from the n.Note field, which is a nVarchar(max) column.


I have the following, but I get an error saying "Invalid length parameter passed to the LEFT or SUBSTRING Function.


Select s.FirstName, s.LastName, c.CustomerNo, h.OppNo, n.Note,
SUBSTRING(SUBSTRING(n.Note,1,CHARINDEX(' ',n.Note,CHARINDEX('@', n.Note))-1),CHARINDEX('@', n.Note) - CHARINDEX(' ',REVERSE(SUBSTRING(n.Note,1,CHARINDEX('@', n.Note)-1))) + 1, 255)
from SalesLink_SalesRep s,
SalesLink_Prospect_SalesRepCustomerXrf c,
Opportunity_Header h,
Opportunity_Note n
where c.SalesRepId = s.SalesRepId
and c.CustomerNo = h.CustomerNo
and h.OppNo = n.OppNo
and s.SalesRepId in (238, 328)
and n.Note like '%@%'
order by CustomerNo;
Post #1402134
Posted Wednesday, January 2, 2013 6:37 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 6:12 PM
Points: 17,626, Visits: 15,482
It looks like you have some data that does not meet the requirements (an invalid email address or the @ symbol is present without an email address).

Can you provide sample data for this field?




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1402138
Posted Wednesday, January 2, 2013 10:41 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:01 PM
Points: 1,287, Visits: 1,122
tbmabry (1/2/2013)
I need to get the emails from the n.Note field, which is a nVarchar(max) column.


I have the following, but I get an error saying "Invalid length parameter passed to the LEFT or SUBSTRING Function.


Select s.FirstName, s.LastName, c.CustomerNo, h.OppNo, n.Note,
SUBSTRING(SUBSTRING(n.Note,1,CHARINDEX(' ',n.Note,CHARINDEX('@', n.Note))-1),CHARINDEX('@', n.Note) - CHARINDEX(' ',REVERSE(SUBSTRING(n.Note,1,CHARINDEX('@', n.Note)-1))) + 1, 255)
from SalesLink_SalesRep s,
SalesLink_Prospect_SalesRepCustomerXrf c,
Opportunity_Header h,
Opportunity_Note n
where c.SalesRepId = s.SalesRepId
and c.CustomerNo = h.CustomerNo
and h.OppNo = n.OppNo
and s.SalesRepId in (238, 328)
and n.Note like '%@%'
order by CustomerNo;


Split each of the function in a separate query and run, then you know which function is not working. Alternately filter the records by customer number till you get error batch then check the data for that batch.
Post #1402180
Posted Wednesday, January 2, 2013 11:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:18 AM
Points: 3,417, Visits: 5,332
Instead of write-only code, I've always found it immensely helpful to split up complex string manipulations for readability into individual parts using cascading CROSS APPLYs like this:

;WITH Notes (Note) AS (
SELECT 'aaa abc@wondwerks.com bbb'
UNION ALL SELECT 'dwain.c@sqlservercentral.com'
UNION ALL SELECT 'dwain.c@'
UNION ALL SELECT '@sqlservercentral.com'
UNION ALL SELECT 'aaa @sqlservercentral.com'
UNION ALL SELECT '@')
SELECT email
FROM Notes
CROSS APPLY (SELECT CHARINDEX('@', Note)) a (PosOfAt)
CROSS APPLY (
SELECT SUBSTRING(Note, 1, PosOfAt - 1)
,SUBSTRING(Note, PosOfAt + 1, LEN(Note))) b(Leading, Trailing)
CROSS APPLY (
SELECT REVERSE(LEFT(REVERSE(Leading), CHARINDEX(' ', REVERSE(Leading) + ' ') - 1)) + '@' +
LEFT(Trailing, CHARINDEX(' ', Trailing + ' ') - 1)) c(email)
WHERE LEFT(email, 1) <> '@' AND RIGHT(email, 1) <> '@'


If you can apply this to your Notes field, you should be able to find only email addresses that contain text on both sides of the @.

Alternatively, you can also use a PATINDEX to isolate only the valid email addresses like this:

;WITH Notes (Note) AS (
SELECT 'aaa abc@wondwerks.com bbb'
UNION ALL SELECT 'dwain.c@sqlservercentral.com'
UNION ALL SELECT 'dwain.c@'
UNION ALL SELECT '@sqlservercentral.com'
UNION ALL SELECT 'aaa @sqlservercentral.com'
UNION ALL SELECT '@')
SELECT Note
FROM Notes
WHERE Note LIKE '%[0-9A-Za-z.][@][0-9A-Za-z.]%'


You'll probably need to add in a few characters to the left [0-9A-Za-z.], like underscore (_), etc. to include all possible valid email address characters.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1402186
Posted Wednesday, January 2, 2013 11:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, January 7, 2013 5:30 PM
Points: 16, Visits: 28
I tried this but it didn't work. I don't know what the emails are going to be they are from all over the U.S. There are over 4000 records that I am going to be pulling from.
Post #1402187
Posted Wednesday, January 2, 2013 11:22 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:18 AM
Points: 3,417, Visits: 5,332
tbmabry (1/2/2013)
I tried this but it didn't work. I don't know what the emails are going to be they are from all over the U.S. There are over 4000 records that I am going to be pulling from.


Since you didn't use the Quote button, it is difficult to tell whose suggestion you tried.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1402188
Posted Wednesday, January 2, 2013 11:39 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 12:26 AM
Points: 2,840, Visits: 3,963
tbmabry (1/2/2013)
I tried this but it didn't work. I don't know what the emails are going to be they are from all over the U.S. There are over 4000 records that I am going to be pulling from.
have you written the below code yourself
SUBSTRING(SUBSTRING(n.Note,1,CHARINDEX(' ',n.Note,CHARINDEX('@', n.Note))-1),CHARINDEX('@', n.Note) - CHARINDEX(' ',REVERSE(SUBSTRING(n.Note,1,CHARINDEX('@', n.Note)-1))) + 1, 255)


IF yes then first only select the innermost function then start overlapping the outer ones .one by one.
this can help you to trace the culprit record.
another thing , first select TOP 100 then TOP 500 and so on , then see which selection is giving error. in this way you can avoid to traverse complete 4000 records.


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1402196
Posted Wednesday, January 2, 2013 11:45 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:18 AM
Points: 3,417, Visits: 5,332
Bhuvnesh (1/2/2013)
tbmabry (1/2/2013)
I tried this but it didn't work. I don't know what the emails are going to be they are from all over the U.S. There are over 4000 records that I am going to be pulling from.
have you written the below code yourself
SUBSTRING(SUBSTRING(n.Note,1,CHARINDEX(' ',n.Note,CHARINDEX('@', n.Note))-1),CHARINDEX('@', n.Note) - CHARINDEX(' ',REVERSE(SUBSTRING(n.Note,1,CHARINDEX('@', n.Note)-1))) + 1, 255)


IF yes then first only select the innermost function then start overlapping the outer ones .one by one.
this can help you to trace the culprit record.
another thing , first select TOP 100 then TOP 500 and so on , then see which selection is giving error. in this way you can avoid to traverse complete 4000 records.


http://rjlipton.wordpress.com/2012/04/04/a-lazy-approach-to-problem-solving/



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1402199
Posted Thursday, January 3, 2013 8:55 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 6:12 PM
Points: 17,626, Visits: 15,482
Tbmabry, did this post by dwain help you?

dwain.c (1/2/2013)
Instead of write-only code, I've always found it immensely helpful to split up complex string manipulations for readability into individual parts using cascading CROSS APPLYs like this:

;WITH Notes (Note) AS (
SELECT 'aaa abc@wondwerks.com bbb'
UNION ALL SELECT 'dwain.c@sqlservercentral.com'
UNION ALL SELECT 'dwain.c@'
UNION ALL SELECT '@sqlservercentral.com'
UNION ALL SELECT 'aaa @sqlservercentral.com'
UNION ALL SELECT '@')
SELECT email
FROM Notes
CROSS APPLY (SELECT CHARINDEX('@', Note)) a (PosOfAt)
CROSS APPLY (
SELECT SUBSTRING(Note, 1, PosOfAt - 1)
,SUBSTRING(Note, PosOfAt + 1, LEN(Note))) b(Leading, Trailing)
CROSS APPLY (
SELECT REVERSE(LEFT(REVERSE(Leading), CHARINDEX(' ', REVERSE(Leading) + ' ') - 1)) + '@' +
LEFT(Trailing, CHARINDEX(' ', Trailing + ' ') - 1)) c(email)
WHERE LEFT(email, 1) <> '@' AND RIGHT(email, 1) <> '@'


If you can apply this to your Notes field, you should be able to find only email addresses that contain text on both sides of the @.

Alternatively, you can also use a PATINDEX to isolate only the valid email addresses like this:

;WITH Notes (Note) AS (
SELECT 'aaa abc@wondwerks.com bbb'
UNION ALL SELECT 'dwain.c@sqlservercentral.com'
UNION ALL SELECT 'dwain.c@'
UNION ALL SELECT '@sqlservercentral.com'
UNION ALL SELECT 'aaa @sqlservercentral.com'
UNION ALL SELECT '@')
SELECT Note
FROM Notes
WHERE Note LIKE '%[0-9A-Za-z.][@][0-9A-Za-z.]%'


You'll probably need to add in a few characters to the left [0-9A-Za-z.], like underscore (_), etc. to include all possible valid email address characters.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1402422
Posted Thursday, January 3, 2013 9:00 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, January 7, 2013 5:30 PM
Points: 16, Visits: 28
Ok, thanks. I found one of them that is making it blow up. It is that the note only has earl@dcande.com with nothing else so no space before the earl part. How do I get around this. I am using the ...


Select s.FirstName, s.LastName, c.CustomerNo, h.OppNo, n.Note,
SUBSTRING(SUBSTRING(n.Note,1,CHARINDEX(' ',n.Note,CHARINDEX('@', n.Note))-1),CHARINDEX('@', n.Note) - CHARINDEX(' ',REVERSE(SUBSTRING(n.Note,1,CHARINDEX('@', n.Note)-1))) + 1, 255)
from SalesLink_SalesRep s,
SalesLink_Prospect_SalesRepCustomerXrf c,
Opportunity_Header h,
Opportunity_Note n
where c.SalesRepId = s.SalesRepId
and c.CustomerNo = h.CustomerNo
and h.OppNo = n.OppNo
and s.SalesRepId in (238, 328)
and n.Note like '%@%'
order by CustomerNo;
Post #1402654
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse