##Function that returns an image value from database##
CREATE FUNCTION dbo.funcDefaultImage
(
@PropertyId INT,
@Position INT
)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @IMAGE VARCHAR(50)
SELECT @IMAGE = T.image
FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY weight DESC) AS [rownum],
PROPERTYGALLERY.image
FROM
PROPERTYGALLERY
WHERE
PROPERTYGALLERY.PropertyId = @PropertyId
) T
WHERE
rownum = @Position
RETURN @IMAGE
END
Result may look like - property_0001.png