Share via

MSSQL Procedure Issue with Large Data set

Ruchita 40 Reputation points
2024-08-07T14:05:33.42+00:00

I have a string as

@DataVal = '52050!52051!52055!52052!52053!52058!52059!52056!52057!51400!51887!51765!50558!51647!50678!!51079!50664!51631!52049!51634!51995!51633!51756!51998!350004386!350004393!350004389!350004385@@5@@Yes@@DN_InUse'

SET @ProjectIDs = PARSENAME(REPLACE(@DataVal, '@@', '.'), 4);

This gives null.

But works if length is smaller.

Below output is the first part i.e.

i.e.

@DataVal = '52050!350004393!350004389!350004385@@5@@Yes@@DN_InUse'


Output is : 52050!350004393!350004389!350004385
SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

Viorel 127K Reputation points
2024-08-07T17:51:05.3033333+00:00

According to documentation, PARSENAME and QUOTENAME cannot work with long strings. Try an alternative:

set @ProjectIDs = substring(@DataVal, 1, charindex('@@', @DataVal) - 1)

Or use STRING_SPLIT on SQL Server 2022.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Yitzhak Khabinsky 27,196 Reputation points
    2024-08-07T19:02:24.1066667+00:00

    Hi @Ruchita,

    As @Viorel already pointed out , the PARSENAME() function can handle strings up to 128 chars.

    Please try the following solution.

    DECLARE @ProjectIDs VARCHAR(1024),
    	@DataVal VARCHAR(1024) = '52050!52051!52055!52052!52053!52058!52059!52056!52057!51400!51887!51765!50558!51647!50678!!51079!50664!51631!52049!51634!51995!51633!51756!51998!350004386!350004393!350004389!350004385@@5@@Yes@@DN_InUse';
    SET @ProjectIDs = LEFT(@DataVal, CHARINDEX('@@', @DataVal) - 1);
    -- test
    SELECT @ProjectIDs AS ProjectIDs;
    
    
    

    SQL Server 2022 and later solution is below.

    SELECT value AS  ProjectIDs
    FROM STRING_SPLIT(REPLACE(@DataVal, '@@', '.'), '.',1)
    WHERE ordinal = 1;
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

Your answer

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