linq to entities - how to query dates that are stored as char? - linq

In the db table the dates are stored in a char(8) field in this format yyyyMMdd.
How do I query for a date range?
I tried the following and it does not work:
context.Where(p=> Convert.ToDateTime(p.Date) >= Convert.ToDateTime('20120411');
context.Where(p=> Convert.ToInt32(p.Date) >= Convert.ToInt32('20120411');
context.Where(p=> int.Parse(p.Date) >= int.Parse('20120411');
From what I've read a possible way is to use EntityFunctions class but I'm not sure how to construct the query. Any ideas on how to do this?

Convert the test string to a date:
context.Where(p=> Convert.ToDateTime(p.Date) >= DateTime.Parse("2012/04/11 00:00:00");

context.Where(p=> p.Date.CompareTo("20120411") >= 0);

Related

Re-write to_char function

When I use to_char function, It's know as internal_function in EP, and Index can't work in this column. Are there any way to re-write it? I don't want to create a function based index
This is my predicates code.
TO_CHAR(createdate,'dd-mm-yyyy') = TO_CHAR(systimestamp,'dd-mm-yyyy')
Many thanks.
Yes, this is exactly the reason we must use purely the date column on one side and a trunc(sysdate) like expression on the other instead of TO_CHAR while doing date comparisions.
where createdate >= trunc(sysdate)
and createdate < trunc(sysdate) + 1

hive pyspark date comparison

I am trying to translate a hiveQL query into pyspark. I am filtering on dates and getting different results and I would like to know how to get the behaviour in pySpark to match that of Hive. The hive query is:
SELECT COUNT(zip_cd) FROM table WHERE dt >= '2012-01-01';
In pySpark I am entering into the interpreter:
import pyspark.sql.functions as psf
import datetime as dt
hc = HiveContext(sc)
table_df = hc.table('table')
DateFrom = dt.datetime.strptime('2012-01-01', '%Y-%m-%d')
table_df.filter(psf.trim(table.dt) >= DateFrom).count()
I am getting similar, but not the same, results in the two counts. Does anyone know what is going on here?
Your code first creates datetime object from date 2012-01-01. Then during filtering the object is replaced with it's string representation (2012-01-01 00:00:00) and dates are compared using lexicographically which filters out 2012-01-01:
>>> '2012-01-01' >= '2012-01-01 00:00:00'
False
>>> '2012-01-02' >= '2012-01-01 00:00:00'
True
To achieve the same result as SQL just remove code with strptime and compare dates using strings.

Oracle date format error

I am working on oracle table to pull the data. In one of the query i am using 'where' to filtering between date.
To_Date(VDATU,'DD-MON-YYYY') >= '03-Jun-2012'
AND To_Date(VDATU,'dd-mon-yy') <= '04-Sep-2012'
I am getting error,
ORA-01861: literal does not match format string
I tried using ('dd-mm-yy') but still it is giving the same error. what may the problem??
You are trying to convert VDATU (which is presumably a VARCHAR2) into a date.
In one place you have DD-MON-YYYY and in the other you have dd-mon-yy.
Which one is it? VDATU can not be valid for both.
You're comparing a DATE with a String, try instead:
To_Date(VDATU,'DD-MON-YYYY') >= To_Date('03-JUN-2012','DD-MON-YYYY')
AND To_Date(VDATU,'dd-mon-yy') <= To_Date('04-SEP-2012','DD-MON-YYYY')
In case VDATU is already a date, you shouldn't convert it to_date, all you have to do is:
VDATU >= To_Date('03-JUN-2012','DD-MON-YYYY')
AND VDATU <= To_Date('04-SEP-2012','DD-MON-YYYY')

Comparing data field to today's date - Oracle

I have a date field called Acceptance_Date.
Acceptance_Date = 10/10/2011 (MM/DD/YYYY)
I have to compare Acceptance_Date to Sysdate and return true or false.
How can I compare dates?
Assuming Acceptance_Date is of type Date:
select decode(Acceptance_Date, trunc(sysdate), 1, 0)
from table
else, add to_date(Acceptance_Date,'mm/dd/yyyy') instead of just Acceptance_Date

DATEDIFF (in months) in linq

select col1,col2,col3 from table1
where(DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4
I want to convert this sql query to LINQ. but I dont know any DateDiff alternative in LINQ. can you please suggest me?
You're looking for SqlMethods.DateDiffMonth.
EDIT: In EF4, use SqlFunctions.DateDiff.
Putting aside your original question for a moment, in your query you use:
where(DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4
This query would always cause a full table scan, since you're comparing the result of a function call against a constant. It would be much better if you calculate your date first, then compare your column value against the calculated value, which would allow SQL to use an index to find the results instead of having to evaluate every record in your table.
It looks like you're trying to retrieve anything within the past 4 months, so in your application code, try calculating the date that you can compare against first, and pass that value into your Linq2Entities expression:
DateTime earliestDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-4);
var results = from t in context.table1
where t.col3 >= earliestDate
select t;
In EF6, the class to use is DbFunctions. See, for example, DbFunctions.DiffMonths.
I solved this problem in another manner: I calculated the date from code:
theDate = Date.Now.AddMonths(-4)
And in EF change the condition:
tblAccount.[State Change Date] < theDate

Resources