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
Related
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.
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
I have written following Query in SQL Server :
SELECT *
FROM AttendanceMaster
WHERE Date between '8/12/2012' AND '8/20/2012'
butt Query result returns '8/13/2012' AND '8/19/2012'
but it doesn't returns result for date 12/8/2012 and for date 8/20/2012
so what is the solution to get the data for above two date ?
Try with following query:
SELECT *
FROM AttendanceMaster
WHERE CAST(Date AS DATETIME)
BETWEEN DATEADD(DAY , -1 ,CAST('2012-08-12' AS DATETIME)) AND
DATEADD( DAY , 1 ,CAST('2012-08-20' AS DATETIME));
If you are using SQL Server 2008, you only need to do something like this:
SELECT *
FROM AttendanceMaster
WHERE CONVERT(DATE, [Date]) BETWEEN CAST('2012-08-12' AS DATE) AND CAST('2012-08-20' AS DATE)
EDIT: I am making 2 assumptions here.
1) That you have indeed confirmed that you have data in your AttendanceMaster table that has [Date] equal to 12th and/or 20th August 2012 - you dont want to look for something that is not even there in the first place ;)
2) That the [Date] column is most probably of DATETIME SQL Server type
its not TO but instead AND
select *
from AttendanceMaster
where Date between '8/12/2012' AND '8/20/2012'
try casting it to date
SELECT *
FROM AttendanceMaster
WHERE CAST([Date] AS DATE) BETWEEN CAST('2012-08-12' AS DATE) AND CAST('2012-08-20' AS DATE)
I would suggest you to use <, <=, >=, or > because they allow more flexibility than BETWEEN and you have the choice to including or exclude the endpoints.
So you can rewrite your query like this
SELECT *
FROM AttendanceMaster
WHERE Date > '8/12/2012 00:00:00' AND Date < '8/21/2012 00:00:00'
Plz note that in the above query the end date is 21st and not 20th.
Also remember that by default SQL Server will take any date like '8/20/2012' AS '8/20/2012 00:00:00' and therefore any datetime greater than this will not appear in result if you use BETWEEN or if you dont specify the exact time stamp.
Hope it helps.
If you give dates '2012-08-12' and '2012-08-20' then it returns the data between '2012-08-12 0:00 AM' and '2012-08-20 0:00 AM' So it does not return the valuesfrom the date '2012-08-12'. So you need to write the target date as '2012-08-21'
If you have the date '2012-08-20' in a variable for ex:
DECLARE #FromDate DATETIME
DECLARE #ToDate DATETIME
SET #FromDate = '2012-08-12'
SET #ToDate = '2012-08-20'
DECLARE #NextDate DATETIME
SET #NextDate=CAST(FLOOR(CAST(dateadd(day,1,#ToDate) AS FLOAT)) AS DATETIME)
/*This Returns the Next Day Date.*/
SELECT *
FROM AttendanceMaster
WHERE Date between #Date AND #NextDate
Or you can write as
DECLARE #FromDate DATETIME
DECLARE #ToDate DATETIME
SET #FromDate = '2012-08-12'
SET #ToDate = '2012-08-20'
SELECT *
FROM AttendanceMaster
WHERE Date between #Date AND CAST(FLOOR(CAST(dateadd(day,1,#ToDate) AS FLOAT)) AS DATETIME)
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".
I have the following piece of code in C# that I use to select some rows from a SQL Server table based on a date criteria.
DateTime From, DateTime To
SqlParameter[] oParam = new SqlParameter[3];
oParam[0] = new SqlParameter("#From", From.Date);
oParam[1] = new SqlParameter("#To", To.Date);
DataTable dt = clsDatabaseHistory.ExecuteReader("SELECT * FROM tblHistory WHERE Date_Requested BETWEEN #From and #To", oParam);
If for example From=18/08/2011 and To=18/08/2011 and there is data in the table tblHistory that has the Date_Requested value as 18/08/2011 the query does not return it.
But if I change the value of To from 18/08/2011 to To=19/08/2011 the query returns me all the values from the table that have a Date_Requested value of 18/08/2011, but none from the 19/08/2011.
How can something like that be possible and what query should I use to return rows where the date field is between date1 and date2.
Something like :
select * rows where datevalue >= date1 and datevalue <= date2
Thank you.
Change your query to use >= and <
select * rows where datevalue >= date1 and datevalue < date2 + 1
I bet your Date_Requested in your table also has some time associated with it - so it probably really is 18/08/2011 14:37 or something like that.
The BETWEEN clause will be selecting anything between 18/08/2011 00:00:00 and 18/08/2011 00:00:00 - basically nothing.
What you need to take into account when working with DATETIME is the fact there's always also TIME involved!
If you want everything for today, you need to use:
BETWEEN `18/08/2011 00:00:00` AND `18/08/2011 23:59:59`
With those two values, you should get all rows with a Date_Requested of today.
OR: in SQL Server 2008 and newer, you could also use the DATE column type which stores date only - no time portion involved. In that case, you should be fine with your query values.
From.Date and To.Date gets the date portion on c# side ignoring the time portion; you need to do something similar on the database side.
Try
"SELECT * FROM tblHistory WHERE cast(Date_Requested as DATE) BETWEEN #From and #To"
to remove the time portion.
EDIT:
as explained in this answer, you could change #To param value to
oParam[1] = new SqlParameter("#To", To.Date.AddDays(1));
You need to account for time (not just date). One way I handle that is like this:
SELECT
...
WHERE CONVERT(varchar(8), date_begin, 112) <= convert(varchar(8), #to, 112)
This converts dates to YYYYMMDD format (with no time), which is very easy to use in <, >, = comparrisons.
Ok, thanks to you all, i've done the following thing
oParam[2] = new SqlParameter("#To", To.Date.AddDays(1));
and used the following select
SELECT * from MyTable WHERE CONVERT(varchar(8), Date_Requested, 112) >= CONVERT(varchar(8), #From, 112) and CONVERT(varchar(8), Date_Requested, 112) < CONVERT(varchar(8), #To, 112)
Datetime variables must be converted to be compared.
Thanks all!