How to iterate data from two dates by using month and year - c#

I have a table Sample with some fields
EmpCode=a,OffID=1, FromDate=2015-01-01, ToDate='2015-01-10'
EmpCode=a,OffID=2, FromDate=2015-01-11, ToDate='2015-01-20'
EmpCode=a,OffID=1, FromDate=2015-01-21, ToDate='2015-01-29'
EmpCode=a,OffID=1, FromDate=2015-01-30, ToDate='2015-02-02'
I have 2 dropdown list to select month and year.I want the count of offID in month=1 and year=2015. how can I get it. See last OffID todate is 2015-02-01

I am assuming you need count of offID for the each month and when that offID belongs to 1st month for both todate and fromdate and also same for the year You can do something like this
Select count(offiD)
FROM tablename
WHERE (MONTH(todate) = 1 AND MONTH(fromDate) =1 )
AND (YEAR(fromDate) = 2015 AND YEAR(todate) = 2015)
If you want to filter it for only one date then just remove the second date filter.
Update
You need to put the single quote in your query if you are going to check with hardcoded values
Your Query
Select count(offiD)
FROM
Offdayallocation
WHERE
(MONTH(2015-01-10) = 1 AND MONTH(2015-01-01) =1 )
AND (YEAR(2015-01-01)= 2015 AND YEAR(2015-01-10) = 2015)
and EmpCode='a'
Change the condition like this,
(MONTH('2015-01-10') = 1 AND MONTH('2015-01-01') = 1 )
AND (YEAR('2015-01-01')= 2015 AND YEAR('2015-01-10') = 2015)
and EmpCode='a'
This should resolve your issue.

Related

Select Count days between two dates using LINQ

I need to get the number of holidays between two dates. I have tried using the query below but I'm getting an incorrect count number.
StartDate
EndDate
01/2/2022
03/2/2022
17/2/2022
19/2/2022
SELECT COUNT(*) FROM table
WHERE StartDate <= '02/2/2022' and EndDate >= '19/2/2022'
how I make this date => '02/2/2022' return count of day that between two dates from the first row from the table and get count day from the second row.
The count must be 5 days.
The simplest way of doing it is to have a table that has one row per day for every day from eg year 2000 to year 2099, then you can:
SELECT COUNT(*)
FROM
cal --your dates table
INNER JOIN h. --your holiday table
ON cal.d BETWEEEN h.start AND h.end
WHERE
cal.d BETWEEN '2022-02-02' AND '2022-02-19'
The join will produce 6 rows, the where trims it to 5, there is your count
Generating the calendar table, or its equivalent (an arbitrary series of dates) is an exercise for the reader.. See something like this

Can we insert some data along with insert value select query in c# winform

I want to insert the date and month (which is in two datetimepicker) along with insert value select query.
I have five columns in my invoice table
Student_id, Amount, Fee_description, issue_date, month
I can insert the values for the first three columns but the remaining two are null for which I don't know where to put the datetimepicker value??
I take a datatimepicker for date and month in the design view of the form
insert into invoice (Student_id, Amount, Fee_description, issue_date, month)
select
student.Student_id,
Fee.Fee_Amount, Fee.fee_type
from
student
join
parent on student.father_nic = parent.father_nic
join
allocate_class on student.Student_id = allocate_class.Student_id
join
class on class.class_id = allocate_class.class_id
join
session on allocate_class.session_id = session.session_id
join
Fee on class.class_id = Fee.class_id
where
session.session_id = 1
and Fee.fee_type = 'Tution Fee'
and student.status = 'active'
Where to add that two that datetimpicker value in the above query?
Sure. It would look something like this:
var cmd = new SqlCommand("insert into invoice (
Student_id,
Amount,
Fee_description,
issue_date,
month
)
select student.Student_id
, Fee.Fee_Amount
, Fee.fee_type
, #issDat
, #mon
from ...", "conn str here");
cmd.Parameters.AddWithValue("#issDat", issueDateDateTimePicker.Value);
cmd.Parameters.AddWithValue("#mon", monthDateTimePicker.Value);
I've used AddWithValue to quickly explain the concept- google for a blog called "can we stop using AddWithValue already" for a long discussion on how to move away from it(it's reasonable in this context as it's not being used for filtering) but for now the concept I'm trying to relate is:
An sql statement can take a fixed value from you:
SELECT 'hello' FROM table
It can also take a fixed parameter you supply:
SELECT #pHello FROM table
Hence adding parameters and filing them with fixed values from your day time pickers (or datetime.Now or whatever) is fine, and what you should be doing to insert fixed values using an INSERT ... SELECT style statement
Side note, it isn't clear if month and issue date are related but if they're the same date then you don't need to store it twice- you can extract the month from a date at any point.

one query with groupby / count / select new

I have to make in c# a query with linq to sql. I can handle it in sql but in linq to
sql is the result not what I wanted to get.
So there is a table with:
a day, in datetime with date and time
and a kind of id
I have to count the ids for each date, the time isn't important. So the result
should be something like:
day: 2013-11-12 amountIDs: 4
People said to me, I can make a select new query and in this query I can set the day
and could count the ids, or I make a group by day. I read similar question, but it doesn't work in my case.
Could somebody help me?
I tried it with the statement below, but the days have to be grouped, so now the output is foreach datetime, like this
day: 12.12.2013 12:00:00 amountIDs: 1
day: 12.12.2013 12:10:10 amountIDs: 1
In sql I made this statement:
SELECT CONVERT(VARCHAR(20), data.dayandtime, 106) AS day, count(data.amountIds) as ids
FROM data
WHERE ( data.dayandtime >= DATEADD(day, -28, getdate()) AND (data.type = 100) AND (data.isSomething = 0) )
GROUP BY CONVERT(VARCHAR(20), data.dayandtime, 106), data.isSomthing and it works.
I saw similar cases, where people made a : from-select-new xyz statement, than I made a view of it and tried to group just the view. Like this
var query = data.GroupBy(g => g.day.Value).ToList();
var qry = from data in dbContext
group data by data.day into dataGrpd
select new
{
day= dataGrpd.Key,
amountIDs= dataGrpd.Select(x => x.Id).Distinct().Count()
};
Check This

Query Dates From SQLServer in C#?

I am building a web application that is going to show the upcoming birthdays of a list of people stored in a SQL Server 2008 DB. I can't figure out how to query all records in the DB that are within 30 days of today's date.
Here's what I have so far:
using (HumanResourcesDB db = newH umanResourcesDB(ConfigurationManager.ConnectionStrings["HumanResourcesDB"].ConnectionString))
{
DateTime todaysDate = DateTime.Now;
List<Employee> record =
(from tab in db.Employees
where tab.Birthday between DateTime.Now and todaysDate.AddDays(30)
select tab).ToList();
this.grdBirthdays.DataSource = record;
this.grdBirthdays.DataBind();
}
of course the "between" and "and" don't work that's what I need filled in. I've searched the net for a little while to no avail. Any help?
Just use a greater than and less than
using (HumanResourcesDB db = newHumanResourcesDB(ConfigurationManager
.ConnectionStrings["HumanResourcesDB"].ConnectionString))
{
List<Employee> record = (from tab in db.Employees
where tab.Birthday >= DateTime.Today
&& tab.Birthday < DateTime.Today.AddDays(31)
select tab).ToList();
this.grdBirthdays.DataSource = record;
this.grdBirthdays.DataBind();
}
Also I should mention that I have used DateTime.Today rather than DateTime.Now because today represents the start of the day as 00:00:00.000. If someones birthday is set to whatever today is at 00:00:00.000 and you have used DateTime.Now (let's assume it's 8:00 in the morning). There birthday will not be included in this range because it is considered before "now".

SQL; Plus + or Minus – Instead of Dates (ASP.NET-C#)

I am Working On ASP.NET_C# Application
I am pulling out some Dates form SQL on a Grid View.
But instead of those Dates, I like to see how mush “Plus +” or “minus –“ It is form Today’s Date.
For Example
Today’s Date is = 11/02/2012
If the Date from SQL is 07/02/2012, I like my Grid to show -4
Or
If the Date from SQL is 17/02/2012, I like my Grid to show +6
Please Help me, How do I go about doing that….
This is My SQL Query; That I am working With
SELECT MAX(AmountPay.DateUpto) AS [ PaidUpTo], TimeTable.Name, TimeTable.Ref, TimeTable.Time11to12 FROM AmountPay FULL OUTER JOIN TimeTable ON AmountPay.Ref = TimeTable.Ref WHERE (TimeTable.Time11to12 = #Time11to12) GROUP BY TimeTable.Name, TimeTable.Ref, TimeTable.Time11to12
AmountPay.DateUpto is the Date I Like see as + or -
I have Been Suggested Datediff Function; and I have Used datediff function for working out things like Age from Date of Birth but I can't work out; how I can use this in the Case
Thanks in Advance
You can use Timespan
DateTime oldDate = DateTime.Now.AddDays(-4);
DateTime today = DateTime.Now;
TimeSpan span = oldDate.Subtract(today);
string diff = span.Days.ToString();
Since you are using MySQL, your select statement should be like:
SELECT DATEDIFF(CURDATE(), MAX(AmountPay.DateUpto)) AS [ PaidUpTo],
TimeTable.Name,
......
DATEDIFF will give you values like -1, 1, -5 ...and your DataGridView should display those values just fine.
SELECT DATEDIFF(day,GetDate(), MAX(AmountPay.DateUpto)) AS [ PaidUpTo], TimeTable.Name, TimeTable.Ref, TimeTable.Time11to12 FROM AmountPay FULL OUTER JOIN TimeTable ON AmountPay.Ref = TimeTable.Ref WHERE (TimeTable.Time11to12 = #Time11to12) GROUP BY TimeTable.Name, TimeTable.Ref, TimeTable.Time11to12

Categories

Resources