SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Date Format

By Ervin Santiago,

Many clients require data to be sent to them in various formats.  Some require fixed length while others require delimited files.  Whatever the case may be the contents of the file also vary. Some may want you to send date information in mm/dd/yy and others yet want to see it as mmddy.  SQL Server has many built in conversion that will accomplish this, but as i came to find out with my most recent client request, some are just not available.  This being the case i came up with this function which will allow you to pass what format you need your date to be and have it converted for you.  My situation was the client wanted to have the date as mmddyy so after creating the function i tested it and eureka!!  Call the function as follows:  Select dbo.fnDateFormat(getdate(),'mmddyy')  The function also allows for special characters such as / and -.

select dbo.fnDateFormat(GETDATE(),'mm/dd/yyyy')

This function allows for a much cleaner code since i am using bcp to generate the fixed length file i wanted to save time and space in my code by not having to write all the code out in a statement, which as we all know, can get very lenghty and confusing.  

Hope this is benefical to all.  Enjoy!!

Total article views: 2029 | Views in the last 30 days: 2
Related Articles


functions causing query to run excessively long


Get Client IP Address in SQL Server

A function for getting Client IP Address in SQL Server


Max length allowed for RETURN in a function

Max allowed length for value returned using RETURN in a user defined function


Concurrent Value Incrementation by ADO Clients

Query / increment counters by concurrent ADO clients



Dynamic Function