views:

39

answers:

4

My database contains the following strings along with others that are similar

Old Type:  New Type: IRP User: dls0835
Old Type: BASE PLATE New Type: IRP User: ter2344

I am trying to not return the first string type but to still return the second string type.

Notice how there is no text after the "Old Type:" in the first string and that there is actually 2 spaces between that and the "New Type" word.

In the second string it has text after "Old Type:" but this text changes according to what the user picks.

Also the "New Type" and "User" can change as well.

I am trying to not Return any of the records where the "Old Type:" does not have any text after it.

I tried the following:

n.[text] NOT LIKE 'Old Type:  New Type: %'

This still returns the blank ones for some reason. I think it may have to due with there being two spaces between Old Type and New Type when Old Type is blank????

Thanks!

A: 

My initial suggestion is to modify your table layout to make the task easier.

Sometimes changing your angle of approach is the simplest solution.

ShaunLMason
I have no choice but to use it this way.
Understood, though that sucks for a lack of a better word.
ShaunLMason
+2  A: 

How about:

WHERE CHARINDEX('Old Type:  New Type:', n.[text]) = 0
Joe Stefanelli
This still returns the records I don't want: Old Type: New Type: BASE PLATE User: rje234
+2  A: 

Try this:

where patindex('%New Type:%', n.text) > 12
Denis Valeev
This works! thanks
@user380432 Combining `patindex` and `substring` you may actually extract necessary parts for further analysis and filtering.
Denis Valeev
A: 

here's an example of what i would do

declare @table table (String varchar(max))

insert into @table values ('Old Type:  New Type: IRP User: dls0835 ')
insert into @table values ('Old Type: BASE PLATE New Type: IRP User: tdl2921 ')


;with cte as
(
select
    SUBSTRING(string,CHARINDEX('Old Type: ',String)+10,CHARINDEX('New Type: ',String)-(CHARINDEX('Old Type: ',String)+10)) as OldType,
    SUBSTRING(string,CHARINDEX('New Type: ',String)+10,CHARINDEX('IRP User: ',String)-(CHARINDEX('New Type: ',String)+10)) as NewType,
    right(string,len(string)-(CHARINDEX('IRP User: ',string)+8)) as IRPUser
from @table
)
select
*
from cte
where OldType<>''
DForck42