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 1st, 2009, 02:22 PM
Registered User
 
Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic DateAdd Datepart Parameter

I want to be able to allow users to select a time unit, which will determine the DateAdd Datepart parameter.

In this instance:
Select the customerID where the customer's age in 50-65 within the time period that runs from @ReportRunDate back to (@ReportRunDate minus the number of time units). The CalcAge function returns an age given a birthdate. I would like the 'd' of the DateAdd Function to be able to be variable (d,w,n,y)... Any ideas?



Declare @MinAge INT, @MaxAge INT, @ReportRunDate DateTime, @UnitsPrior INT, @Units varChar(5)
Set @MinAge = 50
Set @MaxAge = 65
Set @ReportRunDate = '11/24/2009'
Set @UnitsPrior = 365
Set @Units = 'd'


Select CustomerID
From CustomerProfile
Where CalcAge(Birthdate, DateAdd(d,-@UnitsPrior,@ReportRunDate)) BETWEEN (@MinAge-1) AND (@MaxAge-1)
Group By CustomerID
Order By CustomerID
 
Old December 3rd, 2009, 12:45 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

What about wrapping your WHERE clause in CASE statements for d, w, m, y? I'm supposing you'll have a drop-down for Interval and one for Number.

Myself, I'd probably simplify this and just use two sets of dates.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Provide parameter to query for dynamic report Purple_Khoi Access VBA 1 April 22nd, 2009 10:30 AM
dateadd function help iswo Beginning VB 6 1 August 22nd, 2005 12:07 PM
Dynamic Loading of Parameter List from a file erictelcoguy Crystal Reports 1 July 14th, 2005 10:13 AM
Dynamic sub-reports with parameter passing safecoder Crystal Reports 0 October 27th, 2004 12:26 PM
DateAdd function.. arshad mahmood VB Databases Basics 1 July 29th, 2003 07:37 AM





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