Search code examples
stringpostgresqlnhibernatesql-likenamed-parameters

PostgreSQL+NHibernate -> named parameter in LIKE section


In Spring+Hibernate app im trying to use a named parameter pasend query should sum values from xml document saved in database as text.

    SELECT  document_type, 
     SUM(CAST(substring(document_content ,'<ab.*>(.*[0-9])</ab>') as float)) as value, COUNT(*)
     FROM  statistic_data_test 
     WHERE column LIKE :param1::text
     GROUP BY document_type 
     ORDER BY value DESC 

param1 is named parameter passed to program via URL. Whole query and parameter names are dynamically created by user.

code shown above works fine, but i need an ability to find not only exact match, but i cant concatenate stings in query. For now, in one part of code i'm checking if type is text, and if so, i add % marks as suffix and prefix.

What i wont, is move % marks from parameter to query string, but things like

WHERE column LIKE '%'+:param1::text+'%'

turns errors

How can it be done? How to concatenate stings in query?


Solution

  • Change the strategy, keep the goal.

    Use the position function.

    position(:param1::text in column) <> o