简体   繁体   中英

neo4j cypher left padding String in Where Clause

I have a String property in my nodes where the length of the String isn't fix. Now i must search the right node by this property but i get a fixed length value from another System. For Example my Node has the Value '0123' but I get the Information '000123' for searching.

I need a function like left padding with Zeros and this in the Where Clause like

MATCH (a:LABEL) where leftPad(a.property, 6, '0') = '000123' return a LIMIT 1

Is something like this possible with a good Performance?

You could do this:

MATCH (a:LABEL)
WHERE SUBSTRING('00000', 0, SIZE(a.property)) + a.property = '000123'
RETURN a
LIMIT 1;

Or, if all the characters are numeric, then you could do this:

MATCH (a:LABEL)
WHERE TOINT(a.property) = TOINT('000123')
RETURN a
LIMIT 1;

However, it would be even better if you could just store the property value as an integer in the first place, and also compare it to an integer, which would be the fastest. This might be very easy to do, depending on your situation.

MATCH (a:LABEL)
WHERE a.property = 000123
RETURN a
LIMIT 1;

Try it with reduce :

MATCH (a:LABEL) 
WHERE REDUCE(lp='', n in RANGE(0,5-size(a.name)) | lp+'0')+a. a.property = '000123'
RETURN a

or try it with regular expression :

MATCH (a:LABEL)
WHERE a.property =~ '(0){0,3}123'
RETURN a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM