You need to sign in to do that
Don't have an account?
BPOOR
Convert String to Date without 00:00:00
I am using an SOQL in a webservice that is querying the Contact object based on the BirthDate field. The webservice request has the date in the format of 'yyyy-MM-dd',i.e.,1962-01-29 as an example.
I am not able to use this value to query the Contact Object. I tried converting this String Date into Date object. However, it always has 00:00:00 at the end. I tried the below options.
How do I construct a Date object (without time) from the string date '1962-01-29'?
I did search for solutions in various forums and most of them are suggesting the above two solutions only, which does not work. I am not sure whether this behavior was changed in some Salesforce's releases.
I even tried the Date.parse() method. Whatever I do, I always get the Date with 00:00:00 at the end and this is messing up the SOQL query. When the SOQL is executed, I am getting a message saying [Exception: Line 1:343 No Viable Alternative at character '' ]. The SOQL is built as shown below.
If I run this query in workbench, I get the same error message.
Can someone help?
I am not able to use this value to query the Contact Object. I tried converting this String Date into Date object. However, it always has 00:00:00 at the end. I tried the below options.
String strDate = '1962-01-29'; Date birthDate = Date.valueOf(strDate) ==> This results in 1962-01-29 00:00:00 String strDate = '1962-01-29'; Date dob = Date.valueOf(strDate) Datetime dt = Datetime.newInstance(dob.Year(),dob.Month(),dob.day()); Date birthDate = dt.date() ==> This also results in 1962-01-29 00:00:00
How do I construct a Date object (without time) from the string date '1962-01-29'?
I did search for solutions in various forums and most of them are suggesting the above two solutions only, which does not work. I am not sure whether this behavior was changed in some Salesforce's releases.
I even tried the Date.parse() method. Whatever I do, I always get the Date with 00:00:00 at the end and this is messing up the SOQL query. When the SOQL is executed, I am getting a message saying [Exception: Line 1:343 No Viable Alternative at character '' ]. The SOQL is built as shown below.
USER_DEBUG [112]|DEBUG|Query:: SELECT id,LastName,FirstName,BirthDate,MiddleName__c,MailingAddress,OtherAddress,EmploymentStatus__c,Suffix__c,MaritalStatus__c,Gender__c,MailingState__c,HomePhone,MobilePhone,Email FROM Contact WHERE LastName LIKE 'Smith%' AND BirthDate = 1962-01-29 00:00:00 ORDER BY lastName,firstName,BirthDate LIMIT 50
If I run this query in workbench, I get the same error message.
Can someone help?
You can still query the contact record from Salesforce with following:
The date doesn't have to be in this format -- '1962-01-29'
It can definitely be in this format -- 1962-01-29 00:00:00
Her we go,
Output for debug log is below,
Hope this helps!
Yes, this is how it is supposed to work. However, I am getting the error message "No viable alternative character at '' ". Is it because I am constructing the entire query in a String and finally calling the Database.query() method?
Below is working in workbench for me,
Result is:
Id Name
10031I000002CTDnQAO Andy Smith