querydsl - remove whitespace in the middle of a COLUMN
Asked Answered
L

1

5

Is there a way to do this query in querydsl?

SELECT *
FROM table
WHERE replace(column_name, ' ', '') = 'someValue';

The StringPath from the QClass has no .replace() function and it's necessary for some characters (specifically, spaces in the middle) to be removed from column_name before testing it with someValue.

Sample column_name contents: ABC, DEF, AB *

If someValue is ABC, ABC and AB* should appear.

Lapful answered 30/10, 2014 at 9:33 Comment(2)
Do you use Querydsl with JPA or JDBC?Leguminous
@TimoWestkämper We're using JDBC.Lapful
D
7

You can express the replace invocation via

Expressions.stringTemplate("replace({0},'  ','')", columnPath)
Desk answered 3/11, 2014 at 19:8 Comment(2)
This is great! I didn't know this was possible. This is the final predicate: BooleanBuilder b = new BooleanBuilder().andAnyOf( Expressions.stringTemplate("replace({0},' ','')", columnPath).like(someValue.substring(0,2) Expressions.stringTemplate("replace({0},' ','')", columnPath).like(someValue.substring(0,1) + "*"), columnPath.eq(StringUtils.replaceWildcardChar(someValue))); Thanks.Lapful
Hello, how we can do this if I want to replace space, dash and quote char from columnValue?Philharmonic

© 2022 - 2024 — McMap. All rights reserved.