Monday, September 13, 2010

How to find Last index of character in SQL Server 2008


How to find Last index of character in SQL Server 2008

Many times we need last index of specific character in given string (file processing/name processing)
In SQL Server.
Below is the function for it. you just need to run below function in your sql server.
And need to pass to parameter .
1) @strChar= String from which you find the last index of some Character.
2) @strChar = string/Character


CREATE FUNCTION LastIndexOf
(@strValue VARCHAR(4000),
@strChar VARCHAR(50))
RETURNS INT
AS
BEGIN
DECLARE @index INT

SET @index = 0
WHILE CHARINDEX(@strChar, @strValue) > 0
BEGIN
SET @index = @index + CASE WHEN CHARINDEX(@strChar, @strValue) > 1
THEN
(LEN(@strValue) - LEN(SUBSTRING(@strValue,CHARINDEX(@strChar, @strValue) + LEN(@strChar),LEN(@strValue))))
ELSE
1
END
SET @strValue = SUBSTRING(@strValue,CHARINDEX(@strChar, @strValue) + len(@strChar),LEN(@strValue))
END
RETURN @index
END

==============================================

RUN BELOW QUERY will return 6.
select DBO.LastIndexOf('12345.wmv','.')

===========================================

Best way to display default image if specified image file is not found?



Best way to display default image if specified image file is not found?
Many times in grid/repeater or even in image display we have a problem if we don’t find any image related to that ID/Record and to replace “X” sign or image not found message we need to do programming but here is the
Quick solution which saves lot’s of your time and also your server processing time.

Just put following code in your in your image control.

onerror="this.onerror=null;this.src='Noview.jpg'"
Below is the full code for that


Above code first try to found image at “FullLocationImage”,but if it don’t find an image at specified path it will show “NoViewImage”