Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2005, 01:50 PM
Registered User
 
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to willpower
Default SQL date Funcition

Return a date (This is a calculated date based on the input arguments) It need to Take 3 arguments
Starting Date (Date) (Example 10/10/2005)
Increment (integer) (Example è +4 or -3)
Day of the Week (Varchar)
(Example Monday or Thursday)


Example: If I need to calculate the 5th Monday after 10/10/2005 I will call the function like this è

Exec fnCalculateNewDate @startDate= ‘10/10/2005’, @Increment=+5, @DayOfTheWeek=’Monday’

If I need to calculate 4th Sunday before ‘10/10/2005, I will call the function line this

Exec fnCalculateNewDate @startDate= ‘10/10/2005’, @Increment=-4, @DayOfTheWeek=’Sunday’

 
Old December 25th, 2005, 06:50 AM
Authorized User
 
Join Date: Nov 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I have written the function. Just compile it & run with the proper parameters. Please make appropiate exception handling & format in the function.d_Date format should be appropiate according to ur NLS_DATE_FORMAT settings. U can use also TO_CHAR formatting in the function....

================================================== =========
CREATE OR REPLACE FUNCTION DateExample(d_Date IN DATE,
                                       i_Val IN NUMBER,
                                       v_day IN VARCHAR2) RETURN DATE AS
  sSqlQuery VARCHAR2(100);
  d_computedDate DATE;
BEGIN
  IF i_Val > 0 THEN
    sSqlQuery := 'SELECT NEXT_DAY(''' || d_Date || ''',''' || UPPER(v_day) ||
                 ''') + ' || i_val * 7 || ' FROM DUAL';
  ELSE
    sSqlQuery := 'SELECT NEXT_DAY(''' || d_Date || ''',''' || UPPER(v_day) ||
                 ''') - ' || ABS(i_Val) * 7 || ' FROM DUAL';
  END IF;
  EXECUTE IMMEDIATE (sSqlQuery)
    INTO d_computedDate;
  RETURN d_computedDate;
END DateExample;
================================================== =========

Subhendu De
[email protected]
Software Developer
New Delhi, India
 
Old January 4th, 2006, 05:29 PM
Registered User
 
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to willpower
Default

Thanks a lot it works!!! As they say in Star Wars "May the force be with you"






Similar Threads
Thread Thread Starter Forum Replies Last Post
sql date/time keyvanjan Classic ASP Basics 5 July 22nd, 2006 03:20 PM
SQL date functions frankb522 Other Programming Languages 2 June 24th, 2006 09:04 AM
Sql date help morpheus SQL Language 11 June 23rd, 2004 08:48 AM
SQL Server Date sankar SQL Server 2000 2 December 19th, 2003 07:48 AM
Convert String Date to Date for a SQL Query tdaustin Classic ASP Basics 4 July 7th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.