Technical Article

Conversion Functions

,

These functions will allow you to convert from one data type to another.

As well, you can expand your conversion library to perform even more conversions.

Enjoy !-)

CREATE  FUNCTION CStr(@expression sql_variant)
RETURNS varchar(8000)
AS
BEGIN
return convert(varchar(8000), @expression)
END
--EXAMPLE EXECUTION
--SELECT dbo.CStr(1000000.0000) AS OneMillionDollars


CREATE  FUNCTION CBool(@expression sql_variant)
RETURNS bit
AS
BEGIN
declare @RetVal bit

if upper(dbo.CStr(@expression)) = 'TRUE' set @RetVal = 1 
else if upper(dbo.CStr(@expression)) = 'FALSE' set @RetVal = 0 
else if dbo.CInt(@expression) > 0 set @RetVal = 1 
else if dbo.CInt(@expression) <= 0 set @RetVal = 0

return @RetVal
END
--EXAMPLE EXECUTION
--SELECT dbo.CBool('false') AS DoYouHaveOneMillionDollars


CREATE  FUNCTION CInt(@expression sql_variant)
RETURNS int
AS
BEGIN
declare @x decimal(20, 4)
set @x = convert(decimal(20, 4), @expression)

return convert(int, @x)
END
--EXAMPLE EXECUTION
--SELECT dbo.CInt(1000000.0000) AS OneMillionDollarsWholeNumber

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating