Thursday, September 26, 2013

Function ConvertDateDDMMYYY

CREATE  FUNCTION [dbo].[ConvertDateDDMMYYY]
(
-- Add the parameters for the function here
@DateConvert DATETIME,
@IsThaiDate bit
)
RETURNS NVARCHAR(10)
AS
BEGIN
-- Declare the return variable here
DECLARE @DateConv NVARCHAR(10)

-- Add the T-SQL statements to compute the return value here
SET @DateConvert = ISNULL(@DateConvert,GETDATE())

-- SELECT @DateConv = CAST(DATEPART(YYYY,@DateConvert) AS NVARCHAR)
-- + CASE WHEN LEN(DATEPART(MM,@DateConvert)) = 1 THEN '0' + CAST(DATEPART(MM,@DateConvert) AS NVARCHAR)
--   ELSE CAST(DATEPART(MM,@DateConvert) AS NVARCHAR) END
-- + CASE WHEN LEN(DATEPART(dd,@DateConvert)) = 1 THEN '0' + CAST(DATEPART(dd,@DateConvert) AS NVARCHAR)
--   ELSE CAST(DATEPART(dd,@DateConvert) AS NVARCHAR) END

SELECT @DateConv = CASE WHEN LEN(DATEPART(dd,@DateConvert)) = 1 THEN '0' + CAST(DATEPART(dd,@DateConvert) AS NVARCHAR)
  ELSE CAST(DATEPART(dd,@DateConvert) AS NVARCHAR) END
+'/'+ CASE WHEN LEN(DATEPART(MM,@DateConvert)) = 1 THEN '0' + CAST(DATEPART(MM,@DateConvert) AS NVARCHAR)
  ELSE CAST(DATEPART(MM,@DateConvert) AS NVARCHAR) END
+'/'+ CASE WHEN @IsThaiDate = 0 THEN CAST(DATEPART(YYYY,@DateConvert) AS NVARCHAR)
  ELSE dbo.UFN_GetUserLanguage(CAST((DATEPART(YYYY,@DateConvert) + 543)AS NVARCHAR), CAST(DATEPART(YYYY,@DateConvert)AS NVARCHAR)) END
-- Return the result of the function
RETURN @DateConv

END

No comments:

Post a Comment