SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


create function


create function

Author
Message
river1
river1
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11384 Visits: 1737
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
steveb.
steveb.
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25468 Visits: 7195

declare @SearchString varchar(100)
set @SearchString = '*411221211*222'
select charindex('*',@SearchString,CHARINDEX('*',@SearchString,1)+1)


hallidayd
hallidayd
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1176 Visits: 297
IF LEN(@string) - LEN(REPLACE(@string, '*', '')) >= 2
BEGIN
RETURN LEN(@string) - CHARINDEX('*', REVERSE(@string)) + 1
END


Oliiii
Oliiii
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2784 Visits: 777
Use this:

Select CHARINDEX('*',@InputString,CHARINDEX('*',@InputString, 1) + 1)



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

edit: too late Sad
river1
river1
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11384 Visits: 1737
thank you very much
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search