Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: 1864 | Views in the last 30 days: 8
 
Related Articles
FORUM

functions

functions causing query to run excessively long

SCRIPT

Get Client IP Address in SQL Server

A function for getting Client IP Address in SQL Server

FORUM

Max length allowed for RETURN in a function

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

FORUM

Concurrent Value Incrementation by ADO Clients

Query / increment counters by concurrent ADO clients

FORUM

Function

Dynamic Function

Tags
date    
format    
function    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones