Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 DATEPART with variable date part?

Author  Topic 

Dev Null
Starting Member

39 Posts

Posted - 2005-01-06 : 19:21:11
Hi all,

Is it possible to get DATEPART to work with a variable as the date part to be selected? I have three databases which are identical in structure except for the fact that one stored daily data, one weekly, and one monthly. I'd like to write a stored proceedure to fill the tables which I could simply pass 'dayofyear', 'weekofyear', or 'monthofyear' as a paramenter, and then use that parameter in DATEPART
(as in SELECT DATEPART(@param1, timestamp) AS "Year Part")

This obvious solution doesn't seem to work though.

Thanks

nr
SQLTeam MVY

12543 Posts

Posted - 2005-01-07 : 00:51:16
You can use dynamic sql.
or

select
[Year Part] = case @param1 when 'dd' then DATEPART(dd, timestamp) when 'ww' then DATEPART(ww, timestamp) when 'mm' then DATEPART(mm, timestamp) end

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -