Solved

how to set date parts in Date and Time Functions dynamicly

Posted on 2004-08-11
7
368 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 250 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

696 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