Click here to Skip to main content
13,257,667 members (52,610 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 26 Nov 2012

Build a Date Function

, 26 Nov 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Build a Date Function"


Using the original tip I found that passing incorrect dates like dbo.fn_BuildDate( 2012, 11, 32 ); resulted in a date (2012-12-02) without any error messages, allowing the code to continue with an incorrect date.

So I made a function that gives an error when incorrect dates are passed to it. The quickest and easiest way I could find resulted in the following function.

CREATE FUNCTION [dbo].[fn_BuildDate] (@Year int, @Month int, @Day int)
    RETURN CONVERT( DATETIME, CAST( @Year AS VARCHAR ) + RIGHT( '0' + CAST( @Month AS VARCHAR ), 2 ) + RIGHT( '0' + CAST( @Day AS VARCHAR ), 2 ), 112 )

Using the code 

Using the function has not changed:

DECLARE @StartDate Date
SET @StartDate = dbo.fn_BuildDate(2012,11,26)
But when an invalid date is passed:
DECLARE @StartDate Date
SET @StartDate = dbo.fn_BuildDate(2012,11,32)  
You get the following error message:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The oldest date that can be used before getting an out-of-range error is:
DECLARE @StartDate Date
SET @StartDate = dbo.fn_BuildDate(1753,1,1)

Points of Interest  

For the conversion of the date string to datetime I used CONVERT[^] instead of CAST, because with the CONVERT statement it is possible to specify the style (112 for yyymmdd) of the string to be converted. Had I used a CAST then I would have had to make sure the date string looked like '2012-11-26', causing me to use a additional two string concatenations.  But more importantly this conversion is dependent on the NLS settings of the instance the function is being run on, so using CONVERT we determine the style of the string to be converted.


1. - Release
2. - As per Selvin's comments, the function failed for months and days smaller than 10. The function is updated to correctly process these inputs. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

André Kraak
Software Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...


Comments and Discussions

Questionnot working Pin
Selvin26-Nov-12 5:34
memberSelvin26-Nov-12 5:34 
AnswerRe: not working Pin
André Kraak26-Nov-12 6:14
memberAndré Kraak26-Nov-12 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 26 Nov 2012
Article Copyright 2012 by André Kraak
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid