Technical Article

Simple Function to get Date part of DATETIME value

,

This is a simple function I use to get the month/day/year of a datetime value.

Usage:
Select mydb.dbo.udfmmddyyyy(Getdate())

Note: This is easily modified for British/Australian date format.

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[udfmmddyyyy]') 
and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[udfmmddyyyy]
GO

CREATE FUNCTION dbo.udfmmddyyyy  (@dttm DATETIME)
RETURNS nvarchar(10)
AS
BEGIN
   Declare @udfmmddyyyy nvarchar(10)
   SELECT @udfmmddyyyy = RIGHT('0' + CAST(Month(@dttm) AS Varchar(2)), 2) + '/' + RIGHT('0' + CAST(Day(@dttm) AS Varchar(2)), 2) + '/' + CAST(Year(@dttm) AS Varchar(4))
   RETURN(@udfmmddyyyy)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating