Assign date in salesforce. - salesforce

I have written a batch job. In this batch job i have a condition to take date > = 1/1/2012
I am not able to give the date in the query. can you please help me.
global Database.QueryLocator start(Database.BatchableContext BC) {
system.debug('Inside start');
//date mydate = date.valueof(1/1/2012);
return Database.getQueryLocator('select name from opportunity');
}
I have given it in 2 ways
1st is :
took the date into a date field and give the condition as date >= :mydate
(shows an error in debug log as Invalid date: 0 )
and
2nd way when i gave the date their itself as date >=: 1/1/2012
(it shows as error in debug log as unexpected token: / )
can you please help me
Thanks
Anu

You must follow the proper date format
YYYY-MM-DD
select name from opportunity where mydate >= 2012-01-01
More information here

Related

How to get formatted DateTime irrespective of the time zone in salesforce

We use formatGMT() or format() methods of date time object in apex, it returns the formatted date based on the time zone. Although what should be done if we have a static date, such as date of birth that needs to be formatted, irrespective of the local time zone.
In my scenario when using DateTime.format(), The date of birth is showing correctly for users in India, although for the users in US, yesterday's date is displayed in some cases. In some cases it's converted correctly.
For eg. The dob for x person is 1995-09-20 which gets formatted to 09/20/1995,
Whereas for y person, the same method generates a formatted date 05/15/1990 when dob is 1990-05-16.
Basically the formatted date should be the exact replica of the dob, irrespective of time zone.
Method:
private static String localeFormatDate (Date dateValue) {
try{
String format = String.isNotBlank (strDateFormat) ? strDateFormat: 'MM/dd/YYYY';
Date myDate = Date.newInstance (dateValue.year(), dateValue.month(), dateValue.day());
Time myTime = Time.newInstance(0, 0, 0, 0);
return DateTime.newInstanceGMT (myDate, myTime). format GMT (format);
} catch (Exception ex){
return null;
}
}
Additionally, I have also tried to set timezone as Asia/Colombo as the date is correctly formatted in that time zone:
return DateTime.newInstanceGMT(myDate, myTime).format (format, 'Asia/Colombo');
Although I'm not sure if that would generate the correct format date based on the when the method runs.

How to match a specific string inside a string fetched from database in MVC 5?

So I am building a project where I need to fetch date from the database and on the basis of the given month,
as I need to find customers who have birthday on the given month. I have a query that works perfectly fine in the SQL server but I don't get its equivalent in Linq.
So here is my query that runs in SQL
SELECT * FROM CustReg
WHERE DOB LIKE '_____month%'
I need to match that month as it should come in the 6th position of that DOB string.
Demo Database-
Id Name DOB
1 AB 1995-02-20
2 CD 1998-04-13
4 EF 1991-02-15
5 GH 1988-06-8
6 IJ 2000-02-09
Query - Select all Employee whose birthday comes on feb.
Expected Output - AB,EF,IJ.
PS- I have taken datatype of DOB string not Date or DateTime.
I also have month in string (for eg '02' for feb)that I have fetched from the input date.
If I have understood your question right, may be the following code helps.
First create a enum of Months,
public enum Month
{
Jan=1,Feb,Mar,Apr,May,Jun,July,Aug,Sep,Oct,Nov,Dec
}
Now use the following query to get what u wanted,
Month requiredMonth = Month.Feb;
var res = CustReg.Where(x => (Month)Enum.Parse(typeof(Month), x.DOB.Substring(5, 2)) == requiredMonth).Select(x => x.Name);
foreach (var item in res)
{
Console.WriteLine(item);
}

Date error in DateProperty() save via ndb

Strange results on a saved date in GAE (Python with ndb library).
The inbound string from a web form is %a %m/%d/%y formatted (Fri 3/3/17).
That's parsed with datetime.strptime to get a date value.
When it's saved in a DateProperty() field, the value is consistently the day before, at 16:00:00.000 PST.
postDate = datetime.datetime.strptime(self.request.get('date-'+
hashed_id),'%a %m/%d/%y')
logging.info('postDate as Date: %s',postDate)
postDateStr = datetime.datetime.strftime(postDate,'%D')
logging.info('postDateStr: %s',postDateStr)
thisPost = ScheduledPost(id = postID,
...
postDate = postDate,
postDateStr = postDateStr
)
log results:
postDate as Date: 2017-03-03 00:00:00
postDateStr: 03/03/17
so far so good, right? but in the Datastore interface, that record shows:
PostDate: 2017-03-02 (16:00:00:000 PST)
PostDateStr: 03/03/17
Oops.
Workstation is in Pacific time - but leaving that aside - date queries seem to confirm that the date is wrong. Assuming today is 3/3/17 -
today = dt.datetime.now()
ScheduledPost.query(ScheduledPost.postDate == today).fetch()
No record returned.
Saving date as string, and querying on date as string, are feasible workarounds for this project. Just thought it merited posting - anyone seen this? Advice?
The datetime you're generating on appengine is UTC, and it gets stored as that datetime, without timezone information.
When you're viewing it, it's being converted to pacific-time.
The query you're doing is incorrect: you're generating a datetime, not a date, so the time you're comparing with is going to be different.

Importing Dates into R from SQL Server

I am trying to query a SQL Server database to check for the MAX date in one field and to select the next day for input into another process. I am able to query the database and pull back a date, but I can't convert it into a date object.
Here is what I tried:
tmpMaxDate <- sqlQuery(dbhandle, 'select MAX([date]) + 1 from dbo.My_Data ')
tmpMaxDate
1 2016-01-02
IsDate(tmpMaxDate)
[1] FALSE
maxDate <- as.Date(tmpMaxDate)
Error in as.Date.default(tmpMaxDate) :
do not know how to convert 'tmpMaxDate' to class “Date”
maxDate
NULL
IsDate(maxDate)
[1] FALSE
maxDate <- as.Date(tmpMaxDate, format = "%Y-%M-%D")
Error in as.Date.default(tmpMaxDate, format = "%Y-%M-%D") :
do not know how to convert 'tmpMaxDate' to class “Date”
IsDate(maxDate)
[1] FALSE
maxDate
NULL
The packages I am using are RODBC, chron, lubridate, and RSQLserver
I thought I needed to convert to a date object to use the date from SQL Server in R.
Any thoughts on why my date won't convert? Do I need to convert to be able to use that date to filter data in R?
DateVar <- sqlQuery(dbhandle,"SELECT CONVERT(varchar(10),GETDATE() + 1,101) as Date")
DateVar
NewDate <- as.Date(DateVar$Date, format = "%m/%d/%y")
NewDate
I think the trick for me to was give the date response a column name and then reference the column (being the specific string value needed to convert) name in the conversion.
class(NewDate)
will show as "Date"

DATE COMPARISON in GOOGLE APP ENGINE DATASTORE / PYTHON

I am trying to fetch records after a certain date.
I used the below in code:
qstr = "SELECT * FROM Comment where date > '"+str(max_date)+"' order by date desc limit 10"
comments = db.GqlQuery(qstr)
I have the console log I have qStr as follows:
SELECT * FROM Comment where date > '2013-03-07 04:33:31' order by date desc limit 10"
But this does not yield any records (There are records in the DB).
I also tried passing as date time:
comments = db.GqlQuery("SELECT * FROM Comment where date > :1 order by date desc limit 10",
miscUtils._datetime_from_str(max_date))
This also does not yield any results. Can you please let me know as what I'm doing wrong?
(I'm using the following code to convert date string to date http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/)
Also, I tried debugging the statement in App Engine console, and I was not able to do so.
Thanks in advance
I got it to work as follows:comments = db.GqlQuery("select * from Comment where date > :1 order by date desc limit "+MAX_COMMENTS_PER_FETCH_STR, datetime.datetime.strptime(max_date, "%Y-%m-%d %H:%M:%S").date())
Thanks everyone for your help

Resources