Well here is a start. Unfortunatly I could only get it to work as a procedure not a function (where it would be much more useful) but maybe with a bit more time you can get it there.
exec dbo.MyTest 'A','decimal',14,3
exec dbo.MyTest '123.34','decimal',14,3
CREATE PROCEDURE MyTest
(@Var nvarchar(100), @Format nvarchar(50), @Length Int, @Precision Int )
AS
BEGIN
DECLARE @Sql nvarchar (4000)
DECLARE @ReturnVal int
SET @ReturnVal = 1
IF @Length IS NOT NULL
SET @Format = @Format + '('+ CAST(@Length AS VarChar) + ISNULL(','+CAST(@Precision AS VarChar),'') + ')'
SET @Sql = 'BEGIN TRY ' + Char(13) +
' SELECT CAST('''+ @Var + ''' AS ' + @Format + ') ' + Char(13) +
'END TRY' + Char(13) +
'BEGIN CATCH ' + Char(13) +
' SET @ReturnVal = 0 ' + Char(13) +
'END CATCH '
EXEC sp_executeSQL @Sql, N'@ReturnVal Int OUTPUT', @ReturnVal OUTPUT
SELECT @ReturnVal
END
Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]