Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / T-SQL
Tip/Trick

Build a Date Function

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Nov 2012CPOL 5.6K   11   4  
Just a function to make populating a datetime variable easier!

Introduction

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

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

Using the code

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

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

 

Hope it helps someone else! 

License

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


Written By
Software Developer (Senior)
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.

Previously worked for Gemba Solutions Ltd as a Product Development Support Engineer.

Since 2016 I have been working for Currant Ltd, working with jQuery, ASP.NET and C#.

I also started volunteering at a CodeClub which I am enjoying.

Comments and Discussions

 
-- There are no messages in this forum --