Click here to Skip to main content
12,759,844 members (31,048 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 23 Nov 2012

Build a Date Function

, 23 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Just a function to make populating a datetime variable easier!


I have been writing SQL stored procedures for a while now, but whilst I am testing out the code before creating the stored procedure, I always found declaring and setting a date to be a pain!

So I decided to create a function that you enter the year month day and it converts this to a date.

So here it is.... The code

CREATE FUNCTION [dbo].[fn_BuildDate] (@Year int, @Month int, @Day int)
SET @vInputDate = dateadd(day, @Day - 1, dateadd(month, @Month - 1, dateadd(year, @Year-1900, 0)))
RETURN @vInputDate

Using the code

To use the function is nice and easy in comparison to normally casting a date

DECLARE @StartDate Date
SET @StartDate = dbo.fn_BuildDate(2012,11,23)


Hope it helps someone else! 


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


About the Author

Rob Branaghan
Software Developer
United Kingdom United Kingdom
My first memories of programming were back in the days of Amstrad 464+, and we used to get a magazine called "Amstrad Action", every issue had some games or programs you could code into the computer and I used to love doing this.

I remember watching "War Games" at the same time as typing in the code to a small game on the computer, and thought it would be amazing to be able to connect the computer to the telephone!

In 2000, I started a computing A Level, part of the course was Delphi Programming, and I remember writing an application to display train time tables and ticket costs, and creating a manual of how to use it, and the lecturer said to me back then I would have a career as a Manual writer or a programmer. (I chose Programmer)

The programming lecturer was a lively, wacky lecturer, and that made it entertaining.

I then went back to night school in 2006 and learnt VB.NET.

This lecturer (at a different college, in a different country was also quite wacky and lively (seemed to be a common occurrence))

During the second year at night school my employer asked if I could put together a quotation application, I said I was up for it and the MD and I spent a day or two a week looking into the calculations, and after a few months we had created a quotation application that they still use to this day!
I created 4 more applications before joining my previous employer in 2008.

At my previous employment I created Mobile Software for Windows Mobile 6.1, Integrated with Sage Line 50, Exchequer, created ASP.NET websites, SSRS Reports for Customers and Internal use, created the UI for the new Scheduling application, and more.

Now working for Gemba Solutions Ltd as a Product Development Support Engineer.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 23 Nov 2012
Article Copyright 2012 by Rob Branaghan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid