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)
 Case Statement with Input Variable

Author  Topic 

boblarson
Starting Member

17 Posts

Posted - 2008-10-14 : 17:33:14
I have tried all sorts of combinations but I just can't seem to get this one right. I've also searched here and still haven't found a Case statement that is based on an input variable. This is what I have so far:

CREATE FUNCTION dbo.fnGetMonthNumber (@MonthName As Varchar(20))
RETURNS int AS

BEGIN
DECLARE @MonthInt As Int
Select @MonthInt =
CASE @MonthName
When 'January' Then 1
When 'February' Then 2
When 'March' Then 3
When 'April' Then 4
When 'May' Then 5
When 'June' Then 6
When 'July' Then 7
When 'August' Then 8
When 'September' Then 9
When 'October' Then 10
When 'November' Then 11
When 'December' Then 12
ELSE 0
END

Return @MonthInt


I know I have something out of place. Currently I get an
Error 170: Line 23 Incorrect Syntax near '@MonthInt'
error message when checking it.

Thanks for any direction you can give. I'm still picking up on the differences in T-SQL from what I'm used to using.


Thanks,

Bob Larson

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-14 : 17:36:25
CASE WHEN @MonthName = 'January' THEN ... WHEN @MonthName = 'February' ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

boblarson
Starting Member

17 Posts

Posted - 2008-10-14 : 17:47:46
Tara -
Okay, changed it to:

CREATE FUNCTION dbo.fnGetMonthNumber (@MonthName As Varchar(20))
RETURNS int AS

BEGIN
DECLARE @MonthInt As Int
Select @MonthInt =
CASE
When @MonthName = 'January' Then 1
When @MonthName = 'February' Then 2
When @MonthName = 'March' Then 3
When @MonthName = 'April' Then 4
When @MonthName = 'May' Then 5
When @MonthName = 'June' Then 6
When @MonthName = 'July' Then 7
When @MonthName = 'August' Then 8
When @MonthName = 'September' Then 9
When @MonthName = 'October' Then 10
When @MonthName = 'November' Then 11
When @MonthName = 'December' Then 12
ELSE 0
END

Return @MonthInt

And still get the error about "incorrect syntax near '@MonthInt' at line 23 (the last line). And if I remove that line I get "incorrect syntax near 'END'

Thanks for the help.


Thanks,

Bob Larson
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-14 : 17:51:19
You are missing an END. You need one for the BEGIN and also for the CASE.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

boblarson
Starting Member

17 Posts

Posted - 2008-10-14 : 17:59:33
AAAAHHHHH - Gotcha! Thank you for your patience.


Thanks,

Bob Larson
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-15 : 03:13:01
You dont need a seperate function
It is as simple as

declare @MonthName varchar(12)
set @MonthName='March'
select month(cast(@MonthName+' 2000' as datetime)) as month_value


Madhivanan

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

- Advertisement -