Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 97855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:05:05+00:00 2026-05-11T00:05:05+00:00

I want to use a case statement in my user-defined functions because I need

  • 0

I want to use a case statement in my user-defined functions because I need to match on a number of terms. I could use a table for the matches but then I wouldn’t be able to put it inside the Computed Column definition.

This works with IF statements:

CREATE FUNCTION MaraSizeNumber (     @ms varchar ) RETURNS varchar AS BEGIN     IF ms = '16-18' RETURN '1'     ELSE IF ms = '18-20' RETURN '2'     ELSE IF ms = '20-22' RETURN '3'     ELSE IF ms = '22+' RETURN '4'     ELSE IF ms = '24+' RETURN '5'     ELSE IF ms = '14-16' RETURN '7'     ELSE RETURN 'BAD' END 

But with the original style using a CASE…WHEN THEN BLOCK I get an error message.

CREATE FUNCTION MaraSizeCaseExample (     @ms varchar ) RETURNS varchar AS BEGIN     CASE ms         WHEN '16-18' THEN RETURN '1'         WHEN '18-20' THEN RETURN '2'         WHEN '20-22' THEN RETURN '3'         WHEN '22+' THEN RETURN '4'         WHEN '24+' THEN RETURN '5'         WHEN '14-16' THEN RETURN '7'         ELSE RETURN 'BAD'     END END  

I get an error of Incorrect Syntax near case and incorrect syntax near when for my when parts.

I have correctly batched everything up because my last CREATE FUNCTION block ends with the GO, and according to the documentation on CASE, I have the right syntax.

I have a larger scalar function I’m building that will use the other scalar functions to generate the production coding in our system corresponding to other parameters. It would be best to be able to use CASE because the production coding depends on the product and the customer.

I also get an extra error in the second example at my Create Function line that says, ‘Incorrect Syntax: ‘Create Function’ must be the only statement in the batch’, but with everything else identical I don’t get that error with the IFs.

What am I doing wrong, or are CASES only allowed in SQL queries rather than scalar functions? The error messages are coming from SQL Server Management Studio’s squiggle error message system.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:05:05+00:00Added an answer on May 11, 2026 at 12:05 am

    The case statement should look like:

    RETURN CASE @ms        WHEN '16-18' THEN '1'        WHEN '18-20' THEN '2'        WHEN '20-22' THEN '3'        WHEN '22+' THEN '4'        WHEN '24+' THEN '5'        WHEN '14-16' THEN '7'        ELSE 'BAD'   END 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 232k
  • Answers 232k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The best thing to do here is make sure the… May 13, 2026 at 5:40 am
  • Editorial Team
    Editorial Team added an answer Did you run the dev server with the default arguments?… May 13, 2026 at 5:40 am
  • Editorial Team
    Editorial Team added an answer Sure. Just set the last digit of [assembly: AssemblyVersion("1.0.0.0")] to… May 13, 2026 at 5:40 am

Related Questions

I have a winforms (VB 2008) based app that I'm developing and I want
I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I need to keep track of different dates (dynamic). So for a specific Task
I have a UIPickerView on my UIView along with a UITextField . I am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.