DJ Tan DJ Tan - 1 month ago 8
Java Question

SQL Query to search for row value by partial string of the row value

This is my first time asking a question here. Hope you guys can help. I have been trying to do a search function in my app where if for example I search 2016, all dates with the year 2016 would display on JTable from my database. This is my current code and it displays just the columns and no results on JTable:

String searchDate = dateTextField.getText();
String query = "select dateandtime,category,activity from history where dateandtime like '" + searchDate+ "'";


I tried to replace '" + searchDate+ "'" to %'2016'% and it worked. BUt I need to search for other years as well so I would need to query using the text from the textfield. How do I go about doing this so I could use the string entered on the textfield to do the search and come up with results.

Answer

you could do that by adding % before and after the quotes

String query = "select dateandtime,category,activity from history where dateandtime like '%" + searchDate+ "%'";