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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:38:10+00:00 2026-05-15T22:38:10+00:00

I have this great function that parses a field into a pivot table consisting

  • 0

I have this great function that parses a field into a pivot table consisting of four columns so I can sort my table accordingly. My only problem now is I can’t use the function in a query that also calls a “text” datatype to be displayed in the results. Query runs fine as long as I don’t include “spName” which is a “text” datatype. I’ve tried using cast and convert but neither built-in functions work in this query. thanks.

error:

Pivot grouping columns must be comparable. The type of column "spName" is "text", which is not comparable.

query:

SELECT title, recID, spName, [1] AS [Col1], [2] AS [Col2],[3] AS [Col3],[4] AS [Col4] 
FROM (select title, recID, spName from TestTable) t CROSS APPLY dbo.GetNumbers(title) PIVOT (MAX(num) FOR idx IN ([1], [2],[3],[4]) ) AS PivotTable ORDER BY Col1

udf:

CREATE FUNCTION GetNumbers  
( 
@Numbers NVARCHAR(2000)  
) 
RETURNS @Results TABLE  
( 
idx INT IDENTITY(1,1), 
num INT 
)  
AS 
BEGIN 
    DECLARE @NonNumericIndex INT,  @NumericIndex INT 

    SET @NumericIndex = PATINDEX('%[0-9]%',@Numbers) 

    IF (@NumericIndex > 4) --First Column not there 
          INSERT INTO @Results VALUES (NULL) 

    WHILE @NumericIndex > 0 
    BEGIN 
        SET @Numbers = RIGHT(@Numbers,LEN(@Numbers)-@NumericIndex+1) 

        SET @NonNumericIndex = PATINDEX('%[^0-9]%',@Numbers) 

        IF(@NonNumericIndex = 0) 
        BEGIN 
            INSERT 
            INTO @Results VALUES (@Numbers) 
            RETURN 
        END 
        ELSE 
        INSERT 
        INTO @Results VALUES 
            (LEFT(@Numbers,@NonNumericIndex-1)) 

        SET @Numbers = RIGHT(@Numbers,LEN(@Numbers)-@NonNumericIndex+1) 
        SET @NumericIndex = PATINDEX('%[0-9]%',@Numbers) 
    END 
    RETURN 
END 

sample data

title                    recid           spname
QW 1 RT 309-23-1           1             This is title 1 words
QW 1 RT 29-1               2             this is title 2 desc
QW 1 RT 750-1              3             This is title 3 
QW RT 750-1                4             This is title 4 words
  • 1 1 Answer
  • 1 View
  • 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-15T22:38:11+00:00Added an answer on May 15, 2026 at 10:38 pm

    The Text datatype is deprecated and sounds a dubious choice for a name field in any event.

    This works for me

    IF OBJECT_ID('tempdb..#TestTable') IS NULL
    BEGIN
    CREATE TABLE #TestTable
    (
    recid INT PRIMARY KEY,
    title VARCHAR(50),
    spName TEXT
    )
    
    
    INSERT INTO #TestTable
    SELECT 1 AS recid, 'QW 1 RT 309-23-1' AS title, CAST('test1' AS TEXT) AS spName  UNION ALL
    SELECT 2 AS recid, 'QW 1 RT 29-1', CAST('test' AS TEXT) AS spName UNION ALL
    SELECT 3 AS recid, 'QW 1 RT 750-1', CAST('test' AS TEXT) AS spName UNION ALL
    SELECT 4 AS recid, 'QW RT 750-1' , CAST('test' AS TEXT) AS spName
    END
    
    SELECT recid, title, spName, [1] AS [COLUMN 1], [2] AS [COLUMN 2],[3] AS [COLUMN 3],[4] AS [COLUMN 4]
    FROM 
       (SELECT recid, title, CAST(spName AS VARCHAR(MAX)) AS spName FROM #TestTable ) T 
       CROSS APPLY dbo.GetNumbers(title)
    PIVOT 
    (MAX(num) FOR idx IN ([1], [2],[3],[4])
    ) AS PivotTable;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • 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 Prominent open source Clojure projects: Compojure - a web framework… May 16, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer You have plenty of options... You can just override the… May 16, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer As opposed to using 'start-process-shell-command, which just spawns the process,… May 16, 2026 at 3:25 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

Related Questions

I am working with a LINQ to SQL query and have run into an
I'm making some changes to a bit of our app that parses a csv
I have an issue with this jquery code below. Please give me advice or
I have been playing with BeautifulSoup, which is great. My end goal is to
I have used Flex for about a year before deciding that I would rather
This is a strange problem. I have two html pop-up windows, just hidden divs
EDIT: This question is about finding definitive reference to MySQL syntax on SELECT modifying
I'm using Jörn Zaefferers autocomplete plugin for jQuery to enable a live search field,
I have a text file which was created using some Microsoft reporting tool. The
I have a class API which has full code coverage and uses DI to

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.