473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using variables for the parameters in DATEADD

3
Hello,

I am doing something a little weird (I'm withholding the details only so I don't bore anyone, although if I can't find a way to do this with DATEADD, I will probably have to explain more generally what I'm doing).

I am trying to generate a series of dates. These dates are essentially controlled by a parameter that indicates a starting date and a number of dates per year.

Because the number of dates per year may indicate that different intervals between dates would be appropriate, I tried setting a variable (@FreqIntvl) equal to "day", "week" or "month" as necessary and using that variable as the first parameter to the DATEADD function. However, this results in this error message:

Invalid parameter 1 specified for dateadd.

I've tried varying the datatype with which @FreqIntvl is defined, and I've tried enclosing the values assigned to it in quotation marks and not, and nothing seems to help. Is there a way to do what I'm trying to do?

The alternative is to simply write separate sections of code that handle the day, week and month cases, but since those sections of code will be essentially identical except for the words "day", "week" and "month", that seems really ugly.

Any help would be appreciated.

BTW, before I forget: I'm using Transact-SQL in SQL Server 2000 running under (I think) Windows Server 2003.

Thanks,
Tim Kynerd
Aug 28 '07 #1
4 6590
ck9663
2,878 Expert 2GB
Hello,

I am doing something a little weird (I'm withholding the details only so I don't bore anyone, although if I can't find a way to do this with DATEADD, I will probably have to explain more generally what I'm doing).

I am trying to generate a series of dates. These dates are essentially controlled by a parameter that indicates a starting date and a number of dates per year.

Because the number of dates per year may indicate that different intervals between dates would be appropriate, I tried setting a variable (@FreqIntvl) equal to "day", "week" or "month" as necessary and using that variable as the first parameter to the DATEADD function. However, this results in this error message:

Invalid parameter 1 specified for dateadd.

I've tried varying the datatype with which @FreqIntvl is defined, and I've tried enclosing the values assigned to it in quotation marks and not, and nothing seems to help. Is there a way to do what I'm trying to do?

The alternative is to simply write separate sections of code that handle the day, week and month cases, but since those sections of code will be essentially identical except for the words "day", "week" and "month", that seems really ugly.

Any help would be appreciated.

BTW, before I forget: I'm using Transact-SQL in SQL Server 2000 running under (I think) Windows Server 2003.

Thanks,
Tim Kynerd
depending on your environment, setup, configuration,etc... you can either build a select string then do an exec @sqlstatement ..... or you can do a number of CASE..WHEN...
Aug 29 '07 #2
TimK65
3
depending on your environment, setup, configuration,etc... you can either build a select string then do an exec @sqlstatement ..... or you can do a number of CASE..WHEN...
Hmm.

I want to do what I'm doing in a stored procedure, or possibly a function (the whole thing will return a table). Can I do an exec @sqlstatement in that case?

I've already thought of doing this with a set of CASE statements, but again, since almost all the code would be identical, that goes against my programmer grain. :-)

Thanks,
Tim
Aug 29 '07 #3
azimmer
200 Expert 100+
Hmm.

I want to do what I'm doing in a stored procedure, or possibly a function (the whole thing will return a table). Can I do an exec @sqlstatement in that case?

I've already thought of doing this with a set of CASE statements, but again, since almost all the code would be identical, that goes against my programmer grain. :-)

Thanks,
Tim
I suggest you use exec @sqlstatement. It's allowed in stored procedures; if you absolutely need a function you may write a wrapper (a function that only calls the procedure).
Aug 30 '07 #4
TimK65
3
I suggest you use exec @sqlstatement. It's allowed in stored procedures; if you absolutely need a function you may write a wrapper (a function that only calls the procedure).
Thanks to both of you for the suggestions. Ultimately I decided it was easier and more legible (I may not be at this job forever!) to use a CASE WHEN structure for this. It's a bit ugly, but then I had a case that required completely different processing anyway, so I think it's OK.

Best,
Tim
Sep 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ed Garcia | last post by:
I have a form that sends an ASP page the data to use for this string... using values for "startdate", "enddate" and "lookfor" varibables... rs.Open "SELECT * FROM TABLE WHERE dateadded >= ' &...
7
by: simonjpaulger | last post by:
In PHP (and other languages too, im not sure), you can call a function using a variable itself, the variables string contains the function name, which in turn runs. In my situation, i have an...
11
by: sm | last post by:
Hi All, Can anybody give me the syntax to insert a record into SQL server through VB code using variables? The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2,...
5
by: Barkster | last post by:
I have a page that has two functions on it, on load I check for posted value if exists I set some variables that I want to use in the functions but don't want to pass back and forth. Do I need to...
0
by: =?Utf-8?B?Sm9obm55?= | last post by:
I need to get data from a sql server database and insert records into a sqlce database if they do not already exist in the sqlce database. I have written the code below but I'm getting an error...
1
by: Voodoo Jai | last post by:
I have a dbn of images that I retrieve using SQL statements in dreamweaver. I want to get the file name using SQL and then place it into the "getimagesize" function and then use this info...
17
by: R.Rafii | last post by:
Hi, I have a simple (?) question for you all experts. I have a button that performs a query on my SQL and fill a datagrid on the form The code: Dim sconn As New SqlConnection()...
3
by: NatashaB | last post by:
I'm getting this error when I try to insert a variable in a SQL query: 'Exception occurred.', (0, 'Microsoft JET Database Engine', 'No value given for one or more required parameters.' def...
2
by: franc sutherland | last post by:
Hello, I am using Access 2003. Is it possible to use string variables in the INSERT INTO statement? I am using the INSERT INTO statement to add a long list of contacts to a group by looping...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.