Monday, June 29, 2009

Convert Date Format CCYYMMDD to MMDDYYYY/MMDDYY

The following SQL user defined function is used to convert the given date in CCYYMMDD or CCYY/MM/DD format to MM/DD/CCYY.

/*This function returns mm/dd/ccyy given a date in the format ccyymmdd */
/* Input of CCYYMMDD or CCYY/MM/DD will output MM/DD/CCYY */
/* dbo. FuncCCYYMMDD_MM_DD_CCYY(FieldName) */
/* Example: 19780621 = 06/21/1978*/



[CODE]

CREATE FUNCTION dbo.FuncCCYYMMDD_MM_DD_CCYY (@Date varChar(50)) RETURNS varchar(50)
AS
Begin
Declare @Result as varchar(50)
SET @Result = substring(@Date,5,2) + '/' + Substring(@Date,7,2) + '/' + Substring(@Date,1,4)
IF @Result = '00/00/0000'
Set @Result = Null
Return (@Result)
End

[/CODE]



This SQL user defined function is used to convert the given date in the format CCYYMMDD or CCYY/MM/DD into the format MM/DD/YY.


/*This function returns mm/dd/yy given a date in the format ccyymmdd */
/* Input of CCYYMMDD or CCYY/MM/DD will output MM/DD/YY */
/* dbo. FuncCCYYMMDD_MM_DD_YY(FieldName) */
/* Example: 19780621 = 06/21/78*/



[CODE]

CREATE FUNCTION dbo.FuncCCYYMMDD_MM_DD_YY (@Date VARCHAR(50)) RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @Result AS VARCHAR(50)
SET @Result = substring(@Date,5,2) + '/' + Substring(@Date,7,2) + '/' + Substring(@Date,3,2)
IF @Result = '00/00/0000'
SET @Result = NULL
RETURN (@Result)
END

[/CODE]

No comments:

Post a Comment