0

I currently confused at how to set wildcards for long values when using select in databases. Currently I have:

preparedstatement= conn.prepareStatement("SELECT * FROM database WHERE LONGVALUES LIKE ? ");

preparedstatement.setLong(1, aLongValue);

I am currently confused on how to use a wild card to get the results that I want. What I want to select is all values from that database whose LONGVALUES column contains the number aLongValue. So if 52 is in the database entering 5 or 2 would select it.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
Harry S
  • 27
  • 1
  • 1
  • 5
  • 1
    Not sure what the datatype of `LONGVALUES` is. If it's a numeric type, then it doesn't make sense to use a `LIKE` operator with it. If it's a character type, then you shouldn't be using `setLong` in the Java code. – Dawood ibn Kareem Feb 02 '15 at 19:44
  • Seems to be answered here: http://stackoverflow.com/questions/8247970/using-like-wildcard-in-prepared-statement – Palamino Feb 02 '15 at 19:46

2 Answers2

1

You can't do LIKE on a numerical value. It must be a string so you should either make LONGVALUES a VARCHAR field or use a scalar function to convert the value to a string in line with your query i.e.

SELECT * FROM database WHERE TO_CHAR(LONGVALUES) LIKE ?;
robert
  • 4,612
  • 2
  • 29
  • 39
  • So would it be like this? preparedstatement= conn.prepareStatement("SELECT * FROM database WHERE TO_CHAR(LONGVALUES) LIKE ? "); preparedstatement.setChar(1, '%'+aLongValue)+'%'); – Harry S Feb 02 '15 at 19:57
  • I think you would have to construct the parameter when you are doing the setting e.g. `preparedstatement.setString(1, "%" + aLongValue + "%")`. – robert Feb 02 '15 at 20:04
-1

To use LIKE, your aLongValue should be a varchar, then you can run a ... where longvalues like '%5%'

Raul Cosio
  • 110
  • 6
  • 1
    The question remains what is the OP actually trying to do? It doesn't strike me as likely that he should really change the column definition willy-nilly just to suit this possibly ill-defined problem. – user207421 Feb 02 '15 at 21:03