2
select LTRIM(RTRIM(convert(char(10), max(date), 101))) as str 
from   inventory 
where  date < '" + date + "'"

I happen to see this a lot in my Java code. Can someone please re-phrase the query above in plain English? Thank you.

4
  • 2
    A java variable to pass value to the sql query. Easy access for sql injection!
    – atokpas
    Commented Apr 30, 2017 at 1:25
  • @JSapkota Ah! how could this have been better implemented so as to defend against SQL injection?
    – Ebony Maw
    Commented Apr 30, 2017 at 1:35
  • Using prepared statement?
    – atokpas
    Commented Apr 30, 2017 at 1:44
  • 1
    There is a missing " at the start, before the select. Commented Apr 30, 2017 at 10:29

2 Answers 2

3
SELECT LTRIM(RTRIM(CONVERT(char(10), MAX(date), 101))) as str 
FROM   inventory 
WHERE  date < '" + date + "'"

Table: inventory

Field: date (Your inventory table has a field named date)

MAX(date): Returns the highest date according WHERE conditions.

CONVERT(char(10), MAX(date), 101): Convert one data type into another applying some style. On this case 101 means U.S. format: mm/dd/yyyy. This is a MS SQL-Server specific syntax.

RTRIM(): Returns a character string after truncating all trailing spaces.

LTRIM(): Returns a character expression after it removes leading blanks.

Give me the highest date of inventory table, using mm/dd/yyyy format,
without leading and trailing spaces and considering only records
where this date is lower than an specific value. 
1
  • 2
    I should add: while using Microsoft SQL Server, because CONVERT won't work with all SQL databases.
    – joanolo
    Commented Apr 30, 2017 at 21:30
0

In this context, + means concatenation of strings, twice applied to sets of string inputs. + is an overloaded operator in java, which could also mean addition if applied on numeric type inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.