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 6608857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:39:15+00:00 2026-05-25T19:39:15+00:00

Having trouble creating a scalar function inside MS SQL 2005. Any suggestions would be

  • 0

Having trouble creating a scalar function inside MS SQL 2005. Any suggestions would be appreciated. The title1_type field should be set according to the case statement. The sku is passed which should determine the type base on the case statement.

create FUNCTION [dbo].[gettitle1_type] (@sku varchar(50))  
RETURNS varchar(50) AS  
BEGIN 
    DECLARE @title1_type varchar(50)
    DECLARE @title1 varchar(50)
    select @title1_type = case @title1
        when @title1 like '%SMALL%' then 'size'
        when @title1 like '%large%' then 'size'
        when @title1  like '%pink%' then 'color'
        when @title1  like '%red%' then 'color'
        when @title1  like '%brunette%' then 'color'
        else ''
    END;
    where sku = @sku

    RETURN isnull(@title1_type,'')
  • 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. Editorial Team
    Editorial Team
    2026-05-25T19:39:15+00:00Added an answer on May 25, 2026 at 7:39 pm

    You need to

    1. Get rid of the semi colon after END; This is not the end of the statement. Optionally you could put one after where sku = @sku though.
    2. Don’t use case @title1, use Case instead. You are mingling the 2 forms of the CASE expression.
    3. Add a final END to finish the function definition.
    4. Either remove the WHERE or add a FROM
    5. (Optional and dependant on 4) It is best practice to put WITH SCHEMABINDING on scalar UDFs that do not do data access. This can help performance in some cases.

    Giving the below

    CREATE FUNCTION [dbo].[gettitle1_type] (@sku VARCHAR(50))  
    RETURNS VARCHAR(50) 
    WITH SCHEMABINDING
    AS  
    BEGIN 
        DECLARE @title1_type VARCHAR(50)
        DECLARE @title1 VARCHAR(50) /*This is never assigned to ?!*/
    
        SELECT @title1_type = CASE 
            WHEN @title1 LIKE '%SMALL%' THEN 'size'
            WHEN @title1 LIKE '%large%' THEN 'size'
            WHEN @title1  LIKE '%pink%' THEN 'color'
            WHEN @title1  LIKE '%red%' THEN 'color'
            WHEN @title1  LIKE '%brunette%' THEN 'color'
            ELSE ''
        END
        /*
        FROM xyz
        WHERE sku = @sku;*/
    
        RETURN ISNULL(@title1_type,'')
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble creating a mouseover function with an NSTableView. The idea is that
I'm having trouble creating a global function accessible from within all classes. I receive
I'm using LINQ to Entities (not LINQ to SQL) and I'm having trouble creating
I'm having trouble creating a query. I need to create a SQL query where
I have a SQL query I'm having trouble creating using NHibernate Criteria: SELECT ID,
I'm pretty new with JQuery and I'm having trouble creating a behaviour that would
I'm having trouble creating the layout I would like for a website. I don't
I'm having trouble creating a table and putting data pulled from SQL into it.
I am having trouble creating an XML document that contains a default namespace and
I'm currently having trouble creating an image from a binary string of data in

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.