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
 SQL Server Development (2000)
 How to Simulate SQL Server's Enumerations

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-06-13 : 08:52:03
Mike writes "Here is a boiled down example of a more complex scenario:

=====================================
declare @dateType varchar(16)
set @dateType = 'wk'

select datepart (@dateType, getdate())
=====================================

but it results in:

=====================================
Server: Msg 1023, Level 15, State 3, Line 4
Invalid parameter 1 specified for datepart.
=====================================

Of course running the same select statement using what SQL Server is expecting -wk- as the first parameter works fine.

So how can I cast/convert my string into this enumeration or in general any enumerations in SQL Server?

I'm running SQL 2000/2005 on Windows 2003."

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-13 : 09:05:36
1 Use Dynamic SQL

declare @dateType varchar(16)
set @dateType = 'wk'

exec('select datepart ('+@dateType+', getdate())')

2 Use if condition
declare @dateType varchar(16)
set @dateType = 'wk'

if @dateType='wk'
select datepart (wk, getdate())

Second method is advisible to use

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -