Wednesday, March 28, 2012

Please Help for Function

Hi
I am looking for freequently using fuction like Find number of characters in a word.

Ex:
[code]
Declare @.Str='AXG00023'
If I use the function like getnumberofChars(@.Str) Then it has to give the result is 3.
Because rest of all numeric valus.

Is we have any function like this in SqlServer

please Help me

:confused:Yes,

You can use the DATALENGTH(), which will return the number of characters in a string.

If you are using that on a char datatype, you may want to use a Rtrim() on the field first before getting that datalenght.

ie. datalength(rtrim(@.stringname))

Scooter Mcfly|||DECLARE
@.Str varchar(50),
@.i int,
@.x int

SET @.Str ='AXG00X023'
SET @.i = 0
SET @.x=1

WHILE @.x <= DATALENGTH(@.Str) BEGIN
IF ISNUMERIC(SUBSTRING(@.Str,@.x,1)) = 0
SET @.i=@.i+1

SET @.x=@.x+1
END

print @.i

If you are using SQL Server 2000 you could put this code into your own user defined function.

No comments:

Post a Comment