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

  • SEARCH
  • Home
  • 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 818921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:13:30+00:00 2026-05-15T02:13:30+00:00

I am a new with T-SQL. So, please help me to write the sql.

  • 0

I am a new with T-SQL. So, please help me to write the sql.

I have table Price (Code column is primary column):

Code    Value
A1       234
A2       525 
A3       566

I will input a string and the sql need to return a table.

Ex1: input ‘A2’ -> return:

Code    Value
A2       525

Ex2: input ‘A1 A3’ -> return:

Code    Value
A1       234
A3       566

Ex3: input ‘A1 A3 A1’ -> return:

Code    Value
A1       234
A3       566

Ex4: input ‘A1 A4’ -> return:

Code    Value
A1       234

Please help me. I am using SQL Server 2005. Tks.

  • 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-15T02:13:31+00:00Added an answer on May 15, 2026 at 2:13 am
    SELECT [Price].Code, [Price].Value FROM [Price] WHERE [Price].Code IN ('A1', 'A2');
    

    It’s very effective, but it has two limitations:

    • You can’t use regular SQL parameters in an IN clause, so you’ll have append it to your SQL string automatically, which, in some cases, could open SQL Injections.

    • It’s not exactly the input format you requsted: instead of A2 A2 it’s 'A1', 'A2'.

    Good luck anyway!

    EDIT: If you really want to use the A1 A2 format, you can’t use IN and you’d have to split the string and then check if it contains the current [Price].Code. Just note that it will be much less effective than my first example.

    T-SQL doesn’t support Split by default, so you’ll have to add it manually:

    CREATE FUNCTION [dbo].[Split]
    (    
        @RowData NVARCHAR(MAX),
        @Delimeter NVARCHAR(MAX)
    )
    RETURNS @RtnValue TABLE 
    (
        ID INT IDENTITY(1,1),
        Data NVARCHAR(MAX)
    ) 
    AS
    BEGIN 
        DECLARE @Iterator INT
        SET @Iterator = 1
    
        DECLARE @FoundIndex INT
        SET @FoundIndex = CHARINDEX(@Delimeter,@RowData)
    
        WHILE (@FoundIndex>0)
        BEGIN
            INSERT INTO @RtnValue (data)
            SELECT 
                Data = LTRIM(RTRIM(SUBSTRING(@RowData, 1, @FoundIndex - 1)))
    
            SET @RowData = SUBSTRING(@RowData,
                    @FoundIndex + DATALENGTH(@Delimeter) / 2,
                    LEN(@RowData))
    
            SET @Iterator = @Iterator + 1
            SET @FoundIndex = CHARINDEX(@Delimeter, @RowData)
        END
    
        INSERT INTO @RtnValue (Data)
        SELECT Data = LTRIM(RTRIM(@RowData))
    
        RETURN
    END
    

    And then, you could do something like this:

    SELECT [Price].Code, [Price].Value FROM [Price] 
    JOIN Split(@Codes, ' ') AS [Code] 
     ON [Code].Data = [Price].Code
    

    Here’s the source for the Split function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 428k
  • 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 Not safe at all. Yes, it will really compromise the… May 15, 2026 at 1:13 pm
  • Editorial Team
    Editorial Team added an answer I think you just want this: var process = Process.Start(...);… May 15, 2026 at 1:13 pm
  • Editorial Team
    Editorial Team added an answer Zeldman wrote Per HTML and XHTML standards, a DOCTYPE (short… May 15, 2026 at 1:13 pm

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.