Developer forum

Forum » CMS - Standard features » Using datalist to show related items and news in same result.

Using datalist to show related items and news in same result.

Tom-Erik Paulsen
Reply

Hi! I'm wondering if there is a function to split a comma separated string to array for use in the data list SQL query
I can see that this might be a confusing question, but I have to give it a try.

I'm joining two tables and am using data from the url string like this &news=1,2,3&items=69,34,65, representing @Request("news") and @Request("items").

This should be used in an SQL as the example under, but MSSQL needs to convert it to an array of INTs rather than a string if I want to use it in an IN statement.
Does DW have any functions in the SQL server that I can use to insert the @Request("news") and @Request("items") in an "WHERE id IN ()" statement?

 

 

SELECT 
i.Id,
i.Dato,
i.Tittel,
i.Bilde,
i.Kategori,
'item' Type

FROM ItemType_Nyhet i 

    OUTER APPLY
    (
        SELECT TOP 1 *
        FROM Paragraph P
        WHERE i.Id = P.ParagraphItemId
    ) p

WHERE  p.ParagraphShowParagraph = 'true' AND 
GETDATE() > i.dato AND i.dato IS NOT NULL AND 
i.Id IN (@Request("items"))

UNION

SELECT 
n.NewsID Id,
n.NewsCreatedDate Dato,
n.NewsHeading Tittel,
n.NewsSmallImage Bilde,
convert(nvarchar(max),r.CustomFieldRowValue) Kategori,
'news' Type

FROM News n 

    OUTER APPLY
    (
        SELECT TOP 1 *
        FROM AccessCustomFieldRow R
        WHERE n.NewsID = R.CustomFieldRowItemID  AND R.CustomFieldRowFieldID = 21
    ) r

WHERE n.NewsActive = 'true' AND
GETDATE() >= n.NewsActiveFrom AND
GETDATE() <= n.NewsActiveTo AND
n.NewsID IN (@Request("news"))

ORDER BY Dato DESC

 

 

 

 


Replies

 
Nicolai Høeg Pedersen
Reply

Hm, cannot see why it should not work with ID in (1,2,3) format... SQL is always a string, so it makes no sense to parse the string to integers and insert it into the SQL... You would have to do any conversions using functions in SQL/TSQL.

This works fine in my solution:

SELECT * FROM News WHERE NewsID in (1,6,3)

See if anything here can help: http://stackoverflow.com/questions/18371968/sql-variable-to-hold-list-of-integers

 
Tom-Erik Paulsen
Reply

Hi Nicolay!  Using static values in the SQL query har not been a problem. Using @Server:request values has.  As mentioned in the OP, I try to use arguments from the url as comma separated INTs in the IN statement :-)

 
Nicolai Høeg Pedersen
Reply

I know...

But "SELECT * FROM News WHERE NewsID in (@Request("news"))" is changed to "SELECT * FROM News WHERE NewsID in (1,6,3)" before send to the SQL server - and that is basically just a string. And since (1,6,3) is parsed as integers in SQL and not strings, which would have to look like this ("1","6","3"), I do not understand your issue...

 

You must be logged in to post in the forum