handy function for converting date formats

by ssi 30. April 2013 20:31

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE FUNCTION [dbo].[uf_ssi_DateFormat] (@Datetime DATETIME, @Format VARCHAR(32))

RETURNS VARCHAR(32)

AS

BEGIN

    DECLARE @DateValue VARCHAR(32)

   

    SET @DateValue = @Format

   

    IF (CHARINDEX ('YYYY',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue, 'YYYY',

                         DATENAME(YY, @Datetime))

    IF (CHARINDEX ('YY',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue, 'YY',

                         RIGHT(DATENAME(YY, @Datetime),2))

    IF (CHARINDEX ('Month',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue, 'Month',

                         DATENAME(MM, @Datetime))

    IF (CHARINDEX ('MON',@DateValue)>0)

       SET @DateValue = REPLACE(@DateValue, 'MON',

                         LEFT(UPPER(DATENAME(MM, @Datetime)),3))

    IF (CHARINDEX ('Mon',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue, 'Mon',

                          LEFT(DATENAME(MM, @Datetime),3))

    IF (CHARINDEX ('MM',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue,'MM',

                                      RIGHT('0'+CONVERT(VARCHAR,DATEPART(MM, @Datetime)),2))

    IF (CHARINDEX ('M',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue,'M',

                         CONVERT(VARCHAR,DATEPART(MM, @Datetime)))

    IF (CHARINDEX ('DD',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue, 'DD',

                         RIGHT(0+DATENAME(DD, @Datetime),2))

    IF (CHARINDEX ('D',@DateValue) > 0)

       SET @DateValue = REPLACE(@DateValue,'D',

                                     DATENAME(DD, @Datetime))  

RETURN @DateValue

END

 

Ross Mason
Software Development

CADENCE BANK, N.A.
3700 Colonnade Parkway, Suite 200
Birmingham, AL  35243
T 205-949-9716
F n/a

ross.mason@cadencebank.com


Confidentiality Disclosure Notice: The information contained in this email is legally privileged and confidential information for the sole use of the intended recipient. Any use, distribution, transmittal or re-transmittal of information contained in this email by persons who are not intended recipients may be a violation of law and is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

 

 

Tags: ,

SQL

Add comment

biuquote
  • Comment
  • Preview
Loading

Calendar

<<  September 2025  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

RecentComments

None

Development Team @ Shelbysys

We develop custom database applications for our clients. Our development tool of choice is MS Visual Studio. 

Quotations

"Fill the unforgiving minute with sixty seconds worth of distance run."
Rudyard Kipling