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

create function Expand / Collapse
Author
Message
Posted Friday, September 17, 2010 3:05 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Friday, July 11, 2014 8:22 AM
Points: 742, Visits: 1,053
HI,


I need to create a function that receives a parameter (string) and returns a value if there is a second '*' in the string.

E.g:


Function receives the string:

*4112*21211222

returns number 6


Other e.g


Function receives the string:

*411221211*222

returns number 11


Other e.g


Function receives the string:

411221211222

returns nothing, because there is no second *


Can someone help me with the t-sql to do this?

Thnak you






Post #988002
Posted Friday, September 17, 2010 3:17 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
declare @SearchString varchar(100)
set @SearchString = '*411221211*222'
select charindex('*',@SearchString,CHARINDEX('*',@SearchString,1)+1)

Post #988010
Posted Friday, September 17, 2010 3:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 2, 2013 5:45 AM
Points: 112, Visits: 296
IF LEN(@string) - LEN(REPLACE(@string, '*', '')) >= 2
BEGIN
RETURN LEN(@string) - CHARINDEX('*', REVERSE(@string)) + 1
END

Post #988011
Posted Friday, September 17, 2010 3:19 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 7:21 AM
Points: 206, Visits: 764
Use this:
Select CHARINDEX('*',@InputString,CHARINDEX('*',@InputString, 1) + 1)

Returns 0 if it doesn't find a second '*'.

edit: too late
Post #988012
Posted Friday, September 17, 2010 4:00 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Friday, July 11, 2014 8:22 AM
Points: 742, Visits: 1,053
thank you very much
Post #988044
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse