Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.access,microsoft.public.excel.programming,microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 419
Default Recreating the DateSerial()/Date() function

Hello all,

x-posted on:
microsoft.public.access
microsoft.public.excel.programming
microsoft.public.excel.worksheet.functions

Does anyone know the logic behind the DateSerial() function in AC/VBA
(Date() in XL). I want to recreate this function in SQL Server, but have no
idea about how to tackle the logic.

Right now, I have a basic version that will only accept 1-12 for the month
argument and 1-31 for the day argument (I hope no one ever asks for the 31st
of February!!! KABOOM!!!)

I'm fairly certain I can figure out the logic for the month argument so it
will accept negative numbers or 12 and adjust the year accordingly, but I
have no idea how to handle the Day argument to do the same thing. Also, I
don't know which argument needs to get calculated first.

Thanks for any help anyone can provide,

Conan Kelly


  #2   Report Post  
Posted to microsoft.public.access,microsoft.public.excel.programming,microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 4
Default Recreating the DateSerial()/Date() function

Conan Kelly wrote:
Hello all,

x-posted on:
microsoft.public.access
microsoft.public.excel.programming
microsoft.public.excel.worksheet.functions

Does anyone know the logic behind the DateSerial() function in AC/VBA
(Date() in XL). I want to recreate this function in SQL Server, but
have no idea about how to tackle the logic.

Right now, I have a basic version that will only accept 1-12 for the
month argument and 1-31 for the day argument (I hope no one ever asks
for the 31st of February!!! KABOOM!!!)

I'm fairly certain I can figure out the logic for the month argument
so it will accept negative numbers or 12 and adjust the year
accordingly, but I have no idea how to handle the Day argument to do
the same thing. Also, I don't know which argument needs to get
calculated first.
Thanks for any help anyone can provide,


CREATE FUNCTION DateSerial (
@Year int,
@Month int,
@Day int
)

RETURNS DateTime AS

BEGIN
DECLARE @ResultVar DateTime;

IF @Year < 1754 OR @Year 9999
SET @ResultVar = Null;
ELSE
BEGIN
SET @ResultVar = CONVERT(DateTime, '1899-12-1');
SET @ResultVar = DateAdd(year, @Year-1900, @ResultVar);
SET @ResultVar = DateAdd(month, @Month, @ResultVar);
SET @ResultVar = DateAdd(day, @Day-1, @ResultVar);
END

RETURN @ResultVar;
END


--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
updating Outlook contact excel spreadsheets without recreating? summer Excel Discussion (Misc queries) 0 November 29th 07 03:42 PM
update linked spreadsheets w/o recreating links Petersjill Excel Worksheet Functions 1 October 11th 07 09:18 PM
RECREATING THE PROBLEM !!!!!!! FC Excel Discussion (Misc queries) 4 March 8th 07 02:43 AM
Time/DateSerial Numeric Value Jay Excel Worksheet Functions 4 March 16th 05 03:34 AM
How do I change my DSN without recreating the odbc connection? plato Excel Worksheet Functions 0 January 17th 05 05:01 PM


All times are GMT +1. The time now is 11:03 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"