Item order in Correct Format

Hiteshkumar Patel 0 Reputation points
2024-04-22T08:34:08.9866667+00:00

here is my query and query result also i pest what result what i want

SELECT StockGroup [Stock Group], [Item Description], [Balance Bag] [In Stock], [UOM 1], [Balance Stock] [In Stock], [UOM 2], [Usage Per Day], [No of Days In Stock], [Lead Time], [Tolerance Time], [Buffer In Stock], [Required In Stock], [Incoming Qty.], AcceptedDate, [PO Generated], CASE WHEN ISNULL([Incoming Qty.],0)=0 AND AcceptedDate IS NULL AND ([Balance Stock]) < ([Usage Per Day] * ([Lead Time])) THEN 2 WHEN ISNULL([Incoming Qty.],0)=0 AND AcceptedDate IS NULL AND ([Balance Stock]) < ([Usage Per Day] * ([Lead Time] + [Tolerance Time])) THEN 1 WHEN ISNULL([Incoming Qty.],0)> 0 AND AcceptedDate IS NULL AND ([Balance Stock] + ISNULL([Incoming Qty.],0)) < ([Usage Per Day] * ([Lead Time] + [Tolerance Time])) THEN 4 WHEN ISNULL([Incoming Qty.],0)>= 0 AND AcceptedDate IS NOT NULL AND ([Balance Stock] + ISNULL([Incoming Qty.],0)) < ([Usage Per Day] * ([Lead Time] + [Tolerance Time])) THEN 4 WHEN ISNULL([Incoming Qty.],0)>= 0 AND AcceptedDate IS NOT NULL AND ([CONFIRM INComingStock ONTIME]+[CONFIRM INComingStock LATETIME]+[Incoming Qty.]) < ([Usage Per Day] * ([Lead Time] + [Tolerance Time])) THEN 4 WHEN (ISNULL([CONFIRM INComingStock ONTIME],0) + ISNULL([CONFIRM INComingStock LATETIME],0)) > 0 THEN CASE WHEN ISNULL([CONFIRM INComingStock LATETIME],0) = 0 AND ([CONFIRM INComingStock ONTIME] + [Incoming Qty.]) > ([Required In Stock]) THEN 5 WHEN ISNULL([CONFIRM INComingStock ONTIME],0) + ISNULL([CONFIRM INComingStock LATETIME],0) + [Incoming Qty.] > ([Required In Stock]) THEN 6 END ELSE 0 END [Status] FROM MRPBasedOnItemConsumption('" + cmbPurchaseType.SelectedValue.ToString() + "','" + rcmbCompany.SelectedValue.ToString() + "','" + Convert.ToDateTime(dtpFromDate.Value).ToString("yyyyMMdd") + "') ORDER BY StockGroup, [Item Description]

User's image

Result i want is below

User's image

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,991 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 25,956 Reputation points
    2024-04-22T18:26:15.1333333+00:00

    Hi @Hiteshkumar Patel,

    A minimal reproducible example is not provided.

    Here is a conceptual solution for you based on tokenization via SQL Server's XML and XQuery functionality. It will work starting from SQL Server 2017 onwards (due to TRIM() function extended functionality).

    We will tokenize the Item_Description column as XML. After that we will retrieve 7th token, cast it as integer, and sort by it.

    -- DDL and sample data population, start
    DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, Item_Description VARCHAR(200));
    INSERT @tbl VALUES
    ('MATT BOPP FILM [ 20MIC X 575MM ]'),
    ('MATT BOPP FILM [ 20MIC X 1115MM ]'),
    ('MATT BOPP FILM [ 20MIC X 1220MM ]'), 
    ('MATT BOPP FILM [ 20MIC X 675MM ]');
    -- DDL and sample data population, end
    DECLARE @separator CHAR(1) = SPACE(1);
    SELECT *
    FROM @tbl AS t
    CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA[' + 
        REPLACE(Item_Description, @separator, ']]></r><r><![CDATA[') + 
        ']]></r></root>' AS XML)) AS t1(c)
    CROSS APPLY (SELECT TRY_CAST(TRIM('M' FROM c.value('(/root/r[7]/text())[1]', 'VARCHAR(10)')) AS INT)) AS t2(x)
    ORDER BY x ASC;
    
    0 comments No comments

  2. LiHongMSFT-4306 27,961 Reputation points
    2024-04-23T02:24:30.1133333+00:00

    Hi @Hiteshkumar Patel

    Try this:

    ORDER BY 
    StockGroup,
    CAST(SUBSTRING([Item Description],PATINDEX('%[0-9]%', [Item Description]),2)AS int),
    CAST(REPLACE(SUBSTRING([Item Description],CHARINDEX('X',[Item Description])+1,5),'M','')AS int)
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  3. Bruce (SqlWork.com) 66,706 Reputation points
    2024-04-25T18:26:49.8366667+00:00

    your issue is that the order by column is a string. for numeric string values to sort correctly, they need the leading zeros:

    MATT BOPP FILM [20MIC X 575MM]
    MATT BOPP FILM [20MIC X 1115MM]

    s/b

    MATT BOPP FILM [20MIC X 0575MM]
    MATT BOPP FILM [20MIC X 1115MM]

    the best option is to have a sort override column. which you can update with the correct sort description

    order by StockGroup, coalesce(sortOveride,[Item Description])

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.