?
Solved

how to set date parts in Date and Time Functions dynamicly

Posted on 2004-08-11
7
Medium Priority
?
372 Views
Last Modified: 2008-03-10
I have a Table with the following structure

myTable:
datepart (varchar(4)) ,  timeIncrement (int)

I need to increment for each record, the current date based on the fields thah I have described above.

I had tryed this

select DATEADD(datepart , timeIncrement, GETDATE()) AS TheDateINeed
From myTable

however, I recive this message
'datepart' is not a recognized dateadd option.
0
Comment
Question by:lanetas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11776124
Only way I could get it work is as dynamic sql:

DECLARE @sql nvarchar(888)
SELECT @sql = 'SELECT DATEADD(' + datepart + ',' + cast(timeIncrement as varchar(88)) + ', GETDATE()) AS TheDateINeed From myTable'
FROM myTable
EXEC sp_executesql @sql
0
 

Author Comment

by:lanetas
ID: 11776977
It does not work!

I runned it with this info:

myTable:
datepart      timeIncrement
dd      1
year      2
dd      3
      
      
and the result was:

Running dbo."__Tempo".

TheDateINeed            
-----------------------
2004-08-14 1:44:01 PM  
2004-08-14 1:44:01 PM  
2004-08-14 1:44:01 PM  
No more results.
(3 row(s) returned)
@RETURN_VALUE = 0
Finished running dbo."__Tempo".


There are not increments on the current date for each record!
It fix the last datepart      and timeIncrement in the select statement


2004-08-12 1:44:01 PM    (getdate() + 1 day)
2006-08-11 1:44:01 PM    (getdate() + 2 years)
2004-08-14 1:44:01 PM    (getdate() + 3 days)
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11777222
Okay, this will return one set of values, one for each record in myTable:

SET NOCOUNT ON
CREATE TABLE #DateINeed (TheDateINeed datetime NOT NULL)
DECLARE @sql nvarchar(888), @datepart varchar(4), @timeIncrement int

DECLARE Cur1 CURSOR FAST_FORWARD FOR
  SELECT datepart, timeincrement FROM myTable

OPEN Cur1
FETCH NEXT FROM Cur1 INTO @datepart, @timeIncrement
WHILE @@fetch_status = 0
BEGIN
  SELECT @sql = 'INSERT #DateINeed SELECT DATEADD(' + @datepart + ',' + cast(@timeIncrement as varchar(88)) + ', GETDATE())'
  EXEC sp_executesql @sql
  FETCH NEXT FROM Cur1 INTO  @datepart, @timeIncrement
END
CLOSE Cur1
DEALLOCATE Cur1

SELECT TheDateINeed FROM #DateINeed ORDER BY 1
DROP TABLE #DateINeed
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 17

Expert Comment

by:BillAn1
ID: 11777289
try a select statement

select case datepart
              when  'yyyy' then DATEADD(yyyy, timeincrement, getdate())
              when  'yy' then DATEADD(yy, timeincrement, getdate())
              when  'qq' then DATEADD(qq, timeincrement, getdate())
              when  'q' then DATEADD(q, timeincrement, getdate())
              when  'mm' then DATEADD(mm, timeincrement, getdate())
              when  'dd' then DATEADD(dd, timeincrement, getdate())
              when  'dy' then DATEADD(dy, timeincrement, getdate())
              when  'd' then DATEADD(d, timeincrement, getdate())
              when  'hh' then DATEADD(hh, timeincrement, getdate())
              when  'mi' then DATEADD(mi, timeincrement, getdate())
              when  'mn' then DATEADD(mn, timeincrement, getdate())
              when  'ss' then DATEADD(ss, timeincrement, getdate())
end
From myTable
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 11777299
If you have a limited number of dateparts, you could do something "ugly" like this:


SELECT DATEADD(YEAR, CASE WHEN datePart IN ('yy', 'year') THEN timeIncrement ELSE 0 END,
      DATEADD(MONTH, CASE WHEN datePart IN ('mm', 'month') THEN timeIncrement ELSE 0 END,
      DATEADD(DAY, CASE WHEN datePart = 'dd' THEN timeIncrement ELSE 0 END, GETDATE())))
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 750 total points
ID: 11777327
NEVER MIND:

Posted too quickly, the full DATEADD() expression can go in the THEN.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 11777618
Actually, IMO, BillAn1's answer was better.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question