Inside a third-party database dates are stored as strings in format YYYYMMDD.
In SQL I could use the following statement:
WHERE convert(datetime,convert(varchar(8),a.appt_date,112)) = convert(datetime,convert(varchar(8), dateadd(dd,+1,getdate()), 112))
Is there something similar i can do in LINQ to EF? Or is my only option doing it in memory, ex:
dim data = (from item in entities.itemsSet).ToList()
dim filtered = from item in data
where Convert.ToDateTime(shift.starttime) >= startDate
&& Convert.ToDateTime(shift.endtime) < endDate
select item
you should do something like:
dim d = SomeDate
dim d0 = d.Date().ToString("yyyyMMdd")
dim d1 = d.AddAys(1).ToString("yyyyMMdd")
dim filtered = from item in data
where d0 >= startDate
&& d1 < endDate
select item
that is build your string in code and use it as parameter in you query. You will so save lot of conversion time for the same result.
Related
I'm trying to edit this code to be dynamic as I'm going to schedule it to run.
Normally I would input the date in the where statement as 'YYYY-MM-DD' and so to make it dynamic I changed it to DATE(). I'm not erroring out, but I'm also not pulling data. I just need help with format and my google searching isn't helping.
PROC SQL;
CONNECT TO Hadoop (server=disregard this top part);
CREATE TABLE raw_daily_fcast AS SELECT * FROM connection to Hadoop(
SELECT DISTINCT
a.RUN_DATE,
a.SCHEDSHIPDATE,
a.SOURCE,
a.DEST ,
a.ITEM,
b.U_OPSTUDY,
a.QTY,
c.case_pack_qty
FROM CSO.RECSHIP a
LEFT JOIN CSO.UDT_ITEMPARAM b
ON a.ITEM = b.ITEM
LEFT JOIN SCM.DIM_PROD_PLN c
ON a.ITEM = c.PLN_NBR
WHERE a.RUN_DATE = DATE()
AND a.SOURCE IN ('88001', '88003', '88004', '88006', '88008', '88010', '88011', '88012',
'88017', '88018', '88024', '88035', '88040', '88041', '88042', '88047')
);
DISCONNECT FROM Hadoop;
QUIT;
When RUN_DATE is a string you can generate the current date string in-line on the SAS side
WHERE a.RUN_DATE = %str(%')%sysfunc(date(),yymmdd10.)%str(%')
AND ...
or
WHERE a.RUN_DATE = %sysfunc(quote(%sysfunc(date(),yymmdd10.),%str(%')))
AND ...
For the case of RUN_DATE being a string containing DATE9 formatted values, change the yymmdd10. to date9.
change:
WHERE a.RUN_DATE = DATE()
to:
WHERE a.RUN_DATE = PUT(date(), YYMMDD10.) AS date
from p in table
where ID == 201
&& date => 20160601
&& date <= 20160901
select {ID, name};
q.Dump();
The date in the database is in string simple format.
I am trying to convert a SQL query to LINQ. In SQL, BETWEEN operator is being used to select values within a given range. But, BETWEEN can't be used with LINQ statement, so I am getting an error which says => cannot be applied to operands of type string and int for the date field. Any help would be appropriated. I tried the DateTime, but it didn't work for me.
Since LINQ to Entities doesn't support Convert.ToDateTime (why not?) and your date formats are in a reasonable string format, you can compare as strings:
from p in table
where ID == 201
&& date.CompareTo("20160601") >= 0
&& date.CompareTo("20160901") <= 0
select { ID, name };
Try this working code:
var q = from p in table
where p.ID == 201
&& p.date >= DateTime.ParseExact("20160601", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)
&& p.date <= DateTime.ParseExact("20160901", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)
select p;
q.Dump();
How can I update all the tables which have Datetime fields in a database?
Eg. My DB has 50 Tables which has a column in the 50 tables which is of DATETIME Type.
I need to update the Column of datetime type with a new value.
How can I write a generic query to update datetime column in all the tables in the DB with a new value of datetime?
While I question the value of this in an unprecedented way (you should be proud!), you can't write a generic query that does this without dynamic SQL.
DECLARE
#sql NVARCHAR(MAX) = N'',
#newDateTime DATETIME = '20131217 07:30';
SELECT #sql += N'
UPDATE ' + QUOTENAME(s.name) + '.' + QUOTENAME(t.name)
+ ' SET ' + QUOTENAME(c.name) + ' = #newDateTime;'
FROM sys.columns AS c
INNER JOIN sys.tables AS t ON c.[object_id] = t.[object_id]
INNER JOIN sys.schemas AS s ON t.[schema_id] = s.[schema_id]
WHERE c.system_type_id = 61;
EXEC sp_executesql #sql, N'#newDateTime DATETIME', #newDateTime;
why does the following show only the length of the fields inside the grid and not the data?
(column header in grid is also length)
dim q as IQueryable = (from p in DB.Table select p.field1)
while this shows both columns and tha data
dim q as IQueryable = (from p in DB.Table select p.field1, p.field2)
both fields are strings
thanks in advance
What is equal of below sql in LINQ
select MIN(finishTimestamp) AS FromDate, MAX(finishTimeStamp) AS ToDate From Transactions
??
from t in Transactions
select new {
FromDate = ?,
ToDate = ?
}
Thanks
To use multiple aggregates in Linq to SQL, on a table, without grouping, the only way I've found to avoid doing multiple queries, is to make a "fake group":
var q = from tr in dataContext.Transactions
group tr by 1 into g // Notice here, grouping by a constant value
select new
{
FromDate = g.Min(t => t.InvoiceDate),
ToDate = g.Max(t => t.InvoiceDate)
};
Kinda hacky, but the generated SQL is clean, and by doing so, you make only one query to the database.
You can just do
var transactionDates = from t in Transactions
select t.FinishTimeStamp;
var dates = new {
FromDate = transactionDates.Min(),
ToDate = transactionDates.Max()
};
You can also use the aggregate functions if you want (Example in VB)
Dim max = Aggregate tMax In Transactions Select tMax Into Max()