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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:55:13+00:00 2026-05-27T20:55:13+00:00

I have column with the type ntext , and I would like to convert

  • 0

I have column with the type ntext, and I would like to convert it into list of int. Column is called ValidForCustomers and contains Ids of customers comma separated.

I know I can cast it to nvarchar(max) but when I do it SQL throws error

Incorrect syntax near ‘.’.

Part of the query

select Items from Split(c.ValidForCustomers, ',')

This is query

select top 100 
*,c.CouponCode, 
c.Description,
case when c.CouponType = 0 then 'Order - this coupon ONLY applies to the order subtotal'
else 'Product - this coupon ONLY applies to the specified product(s)' end as CouponType,
case when c.DiscountIncludesFreeShipping = 0 then 'No' else 'Yes' end as FreeShiping,
case when c.ExpiresAfterOneUsageByEachCustomer = 0 then 'No' else 'Yes' end as 'ExpiresAfterOneUsageByAnyCustomer',
case when c.ExpiresOnFirstUseByAnyCustomer = 0 then 'No' else 'Yes' end as 'ExpiresOnFirstUseByAnyCustomer',
c.ExpiresAfterNUses,
case when len(CAST(c.ValidForCustomers as nvarchar(max))) = 0 THEN 'No'
    ELSE cstms.CustomerNames END as 'ValidForCustomers',
CASE when LEN(CAST(c.ValidForProducts as nvarchar(max))) = 0 THEN 'No' ELSE 'No' END as 'ValidForProducts',
CASE when LEN(CAST(c.ValidForCategories as nvarchar(max))) = 0 THEN 'No' ELSE 'Yes' END as 'ValidForCategories'
    from Coupon c with(nolock)        
    cross apply ( select CustomerNames = (SELECT SUBSTRING((SELECT ',' + cust.FirstName + ' ' + cust.LastName
                FROM Customer cust
                inner join (select IntegerFromList from dbo.fn_StringListToIntList(CAST(c.ValidFOrCustomers as nvarchar(max)), ',')) sp on (sp.IntegerFromList= cust.CustomerID)                
                ORDER BY cust.FirstName
                FOR XML PATH('')),2,200000))) as cstms

This is procedure for converting

CREATE FUNCTION [dbo].[fn_StringListToIntList]
(
  @IntegerList  NVARCHAR(MAX),
  @Delimiter    CHAR(1) = '|'
)
RETURNS @IntegersTable TABLE (IntegerFromList  INT)
AS
BEGIN
  IF (@IntegerList IS NULL) OR (LEN(@IntegerList) = 0) OR (@Delimiter IS NULL) RETURN

  DECLARE  @DelimPos INT
  SELECT  @DelimPos = PATINDEX('%' + @Delimiter + '%', @IntegerList)

  WHILE @DelimPos <> 0
  BEGIN
    --If nothing between delims, save as NULL
    IF LEN(SUBSTRING(@IntegerList, 1, @DelimPos - 1)) = 0
      INSERT INTO @IntegersTable(IntegerFromList)  VALUES(NULL)
    ELSE
      INSERT INTO @IntegersTable(IntegerFromList)
      VALUES(CONVERT(INT, SUBSTRING(@IntegerList, 1, @DelimPos - 1)))

    SELECT @IntegerList  = SUBSTRING(@IntegerList, @DelimPos + 1, LEN(@IntegerList))
    SELECT @DelimPos  = PATINDEX('%' + @Delimiter + '%', @IntegerList)
  END --While...

  --If no additional chars after a final delim, treat as an additional NULL
  IF LEN(@IntegerList) = 0
    INSERT INTO @IntegersTable(IntegerFromList)  VALUES(NULL)
  ELSE
    INSERT INTO @IntegersTable(IntegerFromList)  VALUES(CONVERT(INT, @IntegerList))

  RETURN 

END --Function

and the error is

Msg 102, Level 15, State 1, Line 17
Incorrect syntax near '('.

and everything is OK with syntax

  • 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-27T20:55:13+00:00Added an answer on May 27, 2026 at 8:55 pm

    I have created custom function for returning string of Customers,

    ALTER FUNCTION [dbo].[fn_GetCustomersForCoupon](
        @CouponID int
    )
    returns nvarchar(MAX)
    as
    Begin
    declare @Str nvarchar(max)
    declare @IntTable table ( ID int)
    declare @ValidForProducts nvarchar(2000)
    
    select @ValidForProducts = ValidForCustomers from Coupon where CouponID=@CouponID
    insert into @IntTable (ID) select * from dbo.fnStringListToIntList(@ValidForProducts, ',')
    SELECT @Str = SUBSTRING((select ',' + Replace(p.FirstName + ' ' + p.LastName, '<Name>','') from @IntTable t
    inner join Customer p on (p.CustomerID = t.ID)
    FOR XML PATH('')),2,200000)
    
    return @Str
    end
    

    now it works as a charm, but directly from query, as subquery it doesn’t work.

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

Sidebar

Related Questions

I have a column of type Bit (called BBoolVal in this example). I have
I have a products table with a column of type SET (called especialidad), with
From my experience I have learn that using an surrogate INT data type column
I would like to have a primary key column in a table that is
I have a column of ntext data type and NOT XML. It stores all
I have a column of ntext data type and NOT XML. It stores all
I have a table in which one column (type ntext) has data that includes
I am working with a DataGridView and have a column of type DataGridViewComboBox and
i created a FTS by this article i have a column with type of
i have a table that has following column Type -------- type 1 type 2

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.