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 '.

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

2 Answers 2

2

String strdate = '12/27/2016';

Date newDate = Date.parse(strdate);

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

1
  • Thank you so much! Been tearing my hair out with this :)
    – Irene
    Commented Oct 7, 2016 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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .