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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:02:50+00:00 2026-06-15T02:02:50+00:00

Need help on how to improve my SQL script for better performance. dbo.Products table

  • 0

Need help on how to improve my SQL script for better performance. dbo.Products table has a million rows. I’m hesitant to rewrite it using dynamic SQL. Thanks!

DECLARE
    @Brand varchar(MAX) = 'Brand 1, Brand 2, Brand 3',
    @ItemCategory varchar(MAX) = 'IC1, IC2, IC3, IC4, IC5'

--will return all records if params where set to @Brand = NULL, @ItemCategory = NULL

SELECT
     [Brand],
     SUM([Amount]) AS [Amount] 
FROM dbo.Products (NOLOCK)
LEFT JOIN [dbo].[Split](@Brand, ',') FilterBrand ON Brand = [FilterBrand].[Items]
LEFT JOIN [dbo].[Split](@ItemCategory, ',') FilterItemCategory ON ItemCategory = [FilterItemCategory].[Items] 
WHERE
    (@Brand IS NULL OR (@Brand IS NOT NULL AND [FilterBrand].[Items]  IS NOT NULL)) AND
    (@ItemCategory IS NULL OR (@ItemCategory IS NOT NULL AND [FilterItemCategory].[Items] IS NOT NULL))
GROUP BY
     [Brand]

Below is the split table-valued function that I found on the web:

CREATE function [dbo].[Split]
(
    @String     varchar(8000),
    @Delimiter  char(1)
)
RETURNS @Results TABLE (Items varchar(4000))
AS
BEGIN
    IF (@String IS NULL OR @String = '') RETURN

    DECLARE @i int, @j int

    SELECT @i = 1

    WHILE @i <= LEN(@String)
        BEGIN
            SELECT  @j = CHARINDEX(@Delimiter, @String, @i)

            IF @j = 0
                BEGIN
                    SELECT  @j = len(@String) + 1
                END

            INSERT  @Results SELECT RTRIM(SUBSTRING(@String, @i, @j - @i))

            SELECT  @i = @j + LEN(@Delimiter)
        END

    RETURN
END
  • 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-06-15T02:02:53+00:00Added an answer on June 15, 2026 at 2:02 am

    Following solution are with out using functions

    Declare @IDs Varchar(100)
    SET @IDs = '2,4,6'
    
    Select IsNull(STUFF((Select ', '+ CAST([Name] As Varchar(100)) From [TableName]
    Where CharIndex(','+Convert(Varchar,[ID])+',', ','+@IDs+',')> 0
    For XML Path('')),1,1,''),'') As [ColumnName]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me to improve the following script: http://jsfiddle.net/n9BkM/8/ I need the following fucntionality:
need help/guide for sql select query, I have 2 table stock and stock_history, in
I need some help trying to improve the speed of my script. Heres the
Need help writing a script downloads data from google insight using c# this is
need help in error in database pivot. i have table tamed table_score like below:
I have one jQuery code and need some help from u guys to improve
I need a help with a small query. Table A:- status and the contents
I'm working on a simple game and I need some help to improve my
I need to improve the performance of a large MySQL view. Most of it
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above

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.