0

I need to use a datetime in a SQL query and it expects it in format 'yyyy-MM-dd 00:00:00'.

I have a string in format 'dd/MM/yyyy'.

How do I get the string formatted as the query wants it? I've tried parse and format but can't seem to get it right. Keep getting the error 'Invalid date/time: 4/10/2016 00:00:00 '.

  • 1
    are you trying this query using SOQL or SQL? – Anshul Oct 6 '16 at 22:03
  • 1
    Could you provide a code or query example? – o-lexi Oct 6 '16 at 22:35
2

String strdate = '12/27/2016';

Date newDate = Date.parse(strdate);

Datetime dt = Datetime.newInstanceGMT(newDate.year(), newDate.month(),newDate.day(),0,0,0);

  • Thank you so much! Been tearing my hair out with this :) – Irene Oct 7 '16 at 0:34
0

The problem is the value is d/mm/yyyy hh:mm:ss, should be 04/10/2016, also this could be because the format is MM/DD/YYYY where the system expects DD/MM/YYYY.

Assuming the field that holds your string dates is called "stringdates".

then you would do

Select CONVERT(varchar(20),stringdates,120)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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