Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

baskar
sharmila
More...


Resources » Code Snippets » SQL

Date format 1 st January 2010


Posted Date:     Category: SQL    
Author: Member Level: Gold    Points: 7


UserDefined function to get the Date format like 1st January 2010



 


There are several dateformat available in Sql Server, but some usable formats
are not available.

This function is designed to get the Date like 1st January 2010


Input Parameters
@GivenDate DATETIME

For this you can pass the value you get from the system function
GETDATE().

CREATE FUNCTION dbo.DateFormat_8th_february_2010
(
@GivenDate DATETIME
)
RETURNS VARCHAR(50)
/*
Created by Alwyn Duraisingh.M on 9th July 2010
Purpose: To get the dateformat like 9 th July 2010
*/
AS
BEGIN
DECLARE @ConvertedDate VARCHAR(50)

SELECT @ConvertedDate =
CASE LEFT(Dates, 2)
WHEN '11' THEN REPLACE(Dates, '11 st', '11 th')
WHEN '12' THEN REPLACE(Dates, '12 nd', '12 th')
WHEN '13' THEN REPLACE(Dates, '13 rd', '13 th')
ELSE Dates END
FROM (
SELECT CONVERT(VARCHAR(2), DAY(@GivenDate)) + ' ' +
CASE RIGHT(CONVERT(VARCHAR(2), DAY(@GivenDate)), 1)
WHEN '1' THEN 'st'
WHEN '2' THEN 'nd'
WHEN '3' THEN 'rd'
ELSE 'th' END + ' ' +
DATENAME(MM, @GivenDate) + ' ' +
CONVERT(VARCHAR(4), YEAR(@GivenDate)) AS Dates
) a
RETURN @ConvertedDate
END

You can execute the function by

SELECT dbo.DateFormat_8th_february_2010(GETDATE())

Result: 9 th July 2010

The result is because i executed the query on 9th July 2010

it will vary according to the current date.
Related Resources:


Read related articles: User control date time    Convert to date format    Date formatting    


Did you like this resource? Share it with your friends and show your love!


Responses to "Date format 1 st January 2010"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to find the duplicate rows in a table using sql Query?
    Previous Resource: Count number of Week Day in a Month
    Return to Resources
    Post New Resource
    Category: SQL


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Dateformat 1st January 2010  .  Date format dd mm yyyy  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.