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.
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 IntSelect @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 12ELSE 0ENDReturn @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 |
|
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 IntSelect @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 12ELSE 0ENDReturn @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 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
boblarson
Starting Member
17 Posts |
Posted - 2008-10-14 : 17:59:33
|
AAAAHHHHH - Gotcha! Thank you for your patience.Thanks,Bob Larson |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-15 : 03:13:01
|
You dont need a seperate functionIt is as simple asdeclare @MonthName varchar(12)set @MonthName='March'select month(cast(@MonthName+' 2000' as datetime)) as month_valueMadhivananFailing to plan is Planning to fail |
|
|
|
|
|