sql query problem - c#

i want to compare two date and time with each other and
i want to select records that "StartTime" is greater than now time and the "StartDate" is greater than now date.
but output is not correct.thanks in advance.
my tabale'records are:
StartDate StartTime
-------------------------------
1389/07/11 11:04
1389/06/23 21:17
1389/06/23 21:32
1389/06/23 22:10
1389/06/26 12:34
1389/06/27 17:29
1389/06/27 18:13
1389/06/27 20:27
1389/06/28 09:41
1389/07/18 10:46
1389/07/05 22:00
1389/07/15 24:00
output is:
when the query is : (1)
SELECT StartDate, StartTime
FROM Proj
WHERE (StartDate < '1389/07/15 ') AND (StartTime <= '20:20 ')
StartDate StartTime
-------------------------------
1389/07/11 11:04
1389/06/26 12:34
1389/06/28 09:41
1389/07/18 10:46
output is:
when the query is: (2)
SELECT StartDate, StartTime
FROM Proj
WHERE (StartDate > '1389/07/15 ') AND (StartTime >= '20:20 ')
StartDate StartTime
-------------------------------
NULL NULL
the correct output should be:
StartDate StartTime
-------------------------------
1389/07/18 10:46
i use persian date.

Just taking what you have above and the description of your problem, the query should be:
select * from test where date>'2010/10/05' and time>='20:22'
If you post more details about your problem and the schema in which you're working we'll be able to help you more.

I want to select records that "time" is greater than now time and the "date" is greater than now date.
First off to get the current datetime (your now), you can use the SQL function GETDATE().
So if you happen to have a datetime column you could just do
SELECT * FROM Test WHERE LogDateTime >= GETDATE()
This will return every record in the table Test of which the datetime value inside the LogDateTime column is in the future.
Now, although it's a little bit more complicated, the same can be used when you have split the date and the time into separate columns.
SELECT * FROM Test
WHERE CONVERT(datetime, LogDate + ' ' + LogTime) >= GETDATE()
If LogDate or LogTime are nullable columns you could use ISNULL(<columnName>, <defaultvalue>) to be safe.

it solved:
SELECT StartDate, StartTime
FROM Proj
WHERE (StartTime < '20:20 ') AND (StartDate = '1389/07/15') OR
(StartDate > '1389/07/15')

Related

T-SQL SQL Server: Compare todays with future date in IF condition

I need to compare todays date with a future date using IF conditional in T-SQL SQL Server. It is contained in a store procedure which is called from C#. Also I want to return no rows if current date is not less or equal than future date.
So I have done:
DECLARE #FutureDate VARCHAR = '2017-05-20 00:00:00'
IF CAST(GETDATE() AS DATE) <= CAST(#FutureDate AS DATE)
BEGIN
....
END
ELSE
BEGIN
SELECT TOP 0 NULL
END
I call this store procedure from C#, But it is generating an error saying cannot convert into datetime. Time is not important for me, only date is important.
UPDATE:
Doing this is working and no need to convert FutureDate as DATE.
DECLARE #FutureDate DATE = '2017-05-20'
IF CAST(GETDATE() AS DATE) <= #FutureDate
BEGIN
....
Doing this is working and no need to convert FutureDate as DATE.
DECLARE #FutureDate DATE = '2017-05-20'
IF CAST(GETDATE() AS DATE) <= #FutureDate
BEGIN
....
END
ELSE
....
END

SQL Server query to get values between fromdate and todate as well as fromdata, todate

SQL Server query to get values between (from date) to (to date) with from date and to date.
I had tried
select *
from abc
where 1 = 1
and entrydate between '"txt1.Text"' and '"txt2.Text"'
I get result between startdate and enddate but in need values of start date and enddate also
If I had a #StartDate parameter and #EndDate parameter both as Datetimes, I would cast them to dates so you can get:
Select *
From ABC
Where entrydate >= CAST(#StartDate as Date)
and entrydate < DATEADD(d, 1, CAST(#EndDate as Date))
Since the end date would be essentially midnight on the morning of the end date, to include it you have to advance it to the next day, then you can do a less than and the previous day is fully included.

DbFunctions DiffDays Gives wrong answer

I want to get a list from DataBase, where MyDate is today or tomarrow.
I wrote the following code.
_Log("Now: " + DateTime.Now.ToString());
var v = db_TS.TS_Test.Where(x => DbFunctions.DiffDays(x.MyDate,DateTime.Now) < 2);
foreach (var item in v.ToList())
{
_Log("MyDate: " + item.MyDate.ToString());
}
The following is logged:
Now: 11/08/2016 10:50:00
MyDate: 27/09/2017 09:35:00
Please help me to find what went wrong in the code?
Thank you
You should be doing DbFunctions.DiffDays(DateTime.Now,x.MyDate) since it's supposed to work like subtracting the first parameter from the second one, so in your case, the DiffDays is returning a negative number.
Summarizing it if you have DbFunctions.DiffDays(date1,date2)
and date1 > date2 the result will be < 0
and date1 < date2 the result will be > 0
Simplest approach is to check year, month, and day with the current date. Month and year should be the same. Day has to be the same for today and one day less for tomorrow:
var v = db_TS.TS_Test.Where(x => x.MyDate.Year == DateTime.Now.Year &&
x.MyDate.Month == DateTime.Now.Month &&
x.MyDate.Day >= DateTime.Now.Day - 1);
Edit
As pointed out this simplistic scenario won't work for edge cases.
Better option is to subtract a date from today and check what's the result in days. If it's tomorrow then the difference is 1 day or 0 if it's today.
var v = db_TS.TS_Test.Where(x => DateTime.Now.Subtract(x.MyDate).Days <= 1);
please check here from more info of datediff function.
Syntax
-- DATEDIFF ( datepart , startdate , enddate )
-- Example usage
SELECT DATEDIFF(DAY, GETDATE(), GETDATE() + 1) AS DayDiff
SELECT DATEDIFF(MINUTE, GETDATE(), GETDATE() + 1) AS MinuteDiff
SELECT DATEDIFF(SECOND, GETDATE(), GETDATE() + 1) AS SecondDiff
SELECT DATEDIFF(WEEK, GETDATE(), GETDATE() + 1) AS WeekDiff
SELECT DATEDIFF(HOUR, GETDATE(), GETDATE() + 1) AS HourDiff
You can play with here

DateTime comparison not including the most recent date

I'm trying to understand why the following function doesn't work.
public IEnumerable<LogFile> GetLogs(string directory, DateTime start, DateTime end)
{
DirectoryInfo di = new DirectoryInfo(directory);
return di.GetFiles("*debug.log").Where(f => f.LastWriteTime > start && f.LastWriteTime <= end).Select(f => new LogFile(f.FullName));
}
Why does the second comparison (f.LastWriteTime <= end) omit the specified end date?
The first comparison (f.LastWriteTime > start) does include the specified start date.
For exampled, if I set the start date to 1/4/2013 and the end date to 1/8/2013 the function return files with the following dates:
1/4/2013,
1/5/2013,
1/6/2013,
1/7/2013
It will not include 1/8/2013, despite the use of <= in the code.
You're dealing with date & time values, not just date values.
1/6/2013 4:30 is not equal to 1/6/2013 12:00, despite the fact that the dates are the same.
You can use the Date property on each of the DateTime objects to get new DateTime objects where the time is always midnight.
DateTime contains (as its name implies) also time component. So your comparison actually is:
f.LastWriteTime > start && f.LastWriteTime <= end
f.LastWriteTime > 1/4/2013 00:00:00 && f.LastWriteTime <= 1/8/2013 00:00:00
The last file date is probably something like 1/8/2013 13:45:12 so
1/8/2013 13:45:12 <= 1/8/2013 00:00:00
is false.
Because of the time component the first date acctualy is included in result:
1/4/2013 00:00:00 > 1/4/2013 13:45:12
is true.
But when compare with date plue time, the last second of value is not included: time <= 1/14/2013 1:26:42 am, it include 1/14/2013 1:26:41 AM ?

Date range falling between two dates in a LINQ query

I'm trying to write a select query which returns records where the input date range falls between two date fields in a LINQ query.
My inputs are:
date1 - start date
date2 - end date
My database fields are
AppointmentStart
AppointmentEnd
Additionally, I'd also like to ensure that an input of 14:00 - 15:00 doesn't return a value for 15:00-16:00.
return (from t1 in db.Appointments where (t1.AppointmentStart <= date2 && (t1.AppointmentEnd) >= date1)
If anybody can assist me with this, I'd appreciate it.
I'm not 100% clear on your requirements. In your opening line you asked for records "where the input date range falls between two date fields", but in the "Additionally" line you imply that you don't want to return records where the start date of the appointment doesn't equal the end date of your input. I take these to be two different requirements, so I'll give you two different queries.
The first query is:
from t1 in db.Appointments
where date1 >= t1.AppointmentStart
where date2 <= t1.AppointmentEnd
select t1;
The second query is:
from t1 in db.Appointments
where date2 > t1.AppointmentStart
where date1 < t1.AppointmentEnd
select t1;
The first query returns records that "contain" the input dates.
The second query returns records that "overlap" the input dates.
I think it makes more sense that you want the overlap query and this one will meet your "14:00 - 15:00 doesn't return a value for 15:00-16:00" requirement.
Let me know if I made a mistake understanding your requirements and need to make any changes.
It looks backwards to me.
if the following is true:
Date1 = start
Date2 = end
then i would think startdate after or equal to appointmentstart and enddate before or equal to appointmentend or:
return (from t1 in db.Appointments where (date1 >= t1.AppointmentStart && date2 <= t1.AppointmentEnd))
i also changed the parens because they didn't make sense to me (seemed like one was missing)

Categories

Resources