I'm trying to display in a graph (Winforms/C#) the total amount from one column vs unit of time (in this case month) - so it would be a amount vs time graph. The problem is that the user would like the freedom of lets say - choosing the totals for January and June and compare them in a single graph (so the total for the month of January would be represented as a bar next to June's total's bar). I already capture the selected months (also, I have the graph control on the for) within a list but where I am really stuck is to build the mysql statement and its something like this
selectdataforGraph = "SELECT SUM(Amount_Net) AS Total FROM testingproject.incomeinformation WHERE date";
foreach (int month in selectedMonth) {
selectdataforGraph += "between '" + selected_year+ "-" + month +
"'-1 AND '" + selected_year + "-"+month+ "-31' AND";
}
I know it has some space missing and some quotation mark problems - already ran the query and I figured as much but I don't think the in-between would work because I don't know how to AND the next part of it so if a user picks May then August would be between 2007-5-01 and 2007-5-30 AND 2007-8-01 and 2007-8-30???
EDIT: didn't seem MySQL was your DB...
Definitely use a parameterized query! However... to fit in with what you have and so you can test it quickly...
I think I would use DATEPART rather than BETWEEN....
var selectdataforGraph = "SELECT SUM(Amount_Net) AS Total FROM testingproject.incomeinformation WHERE ";
var monthList = string.Join(",", selectedMonth);
selectdataforGraph += " YEAR(date) = " + selected_year;
selectdataforGraph += " AND MONTH(date) in (" + monthList + ")";
Related
I need help with my comboboxes.
An application to the value selected in the first combo imported dates in the second and the third combo combo. Only the dates look like this:2009-01-01 12:00:00AM. I want to cut 12:00:00AM. My query is:
string command2 = "select God_MinQ,God_AverQ,God_MaxQ,min(Dat) from hydgod where station='"
+ comboBox1.SelectedItem.ToString() + "' and Dat between '"
+ comboBox2.SelectedItem.ToString() + "' and '" + comboBox3.SelectedItem.ToString()
+ "' group by year(dat),month(Dat)";
Where can I format string in combobox2 and combobox3?
Another thing I wanted to ask you. When the user chooses from combo2 starting date to date as 2009-01-01 to 2010-01-01 Mesagebox out in year 2010, and he has chosen for the year 2009. How can I fix this.
Here is a link to the screenshot:
https://s30.postimg.org/60nuoocdd/Untitled.jpg
If your comboboxes containe DateTime-Values you can format them like this:
DateTime value = (DateTime)comboBox2.SelectedItem;
String valueString = value.ToString("MMMM dd, yyyy");
If you need a differen formationg check out the MSDN Entry
You should really consider using Sql Parameter like #Reniuz suggested.
This prevents SQL Injection attack and helps in general by better readability of your code and easier parsing of values
In my view, I have an input and select tags for the user to enter a start and end date.
When submitted, The controller will search the Model/Database and return entries within the above range.
In my DB, the start and end dates are written as "nvarchars" and in my controller they are taken as strings
Code and Images for reference:
public ActionResult timePeriod(string time)
{
//Start: month, day, year End: month, day, year --> Numeric values
string[] times = time.Split(',');
string start = times[0] + " " + times[1] + " " + times[2];
string end = times[3] + " " + times[4] + " " + times[5];
//Sample code to test the start date
viewModel.Tasks = db.Tasks.Where(s => s.StartTime.Contains(start)).ToList();
}
a snippet of the Database values:
Are there any LINQ expression to do this?
As the dates are strings, you've nothing better other than using what you have suggested already:
viewModel.Tasks = db.Tasks.Where(s => s.StartTime.Equals(start)).ToList();
I would use Equals as that will be quicker for you. Using Contains is basically like doing a T-SQL LIKE which is much slower.
SELECT *
FROM Table
WHERE StartDate LIKE 'blah'
Using Equals will result in the following equivalent:
SELECT *
FROM Table
WHERE StartDate = 'blah'
Which is much more efficient.
I want to compare date entering from a combobox as string with the date that saved into SQL Server database,then summation some values X but comparison doesn't work ...
string from = (comboBox4.Text+ '/' + comboBox5.Text + '/' + comboBox6.Text+ " 00:00:00 AM");
string to = (comboBox1.Text+ '/' + comboBox2.Text + '/' + comboBox3.Text);
cmd.CommandText = "SELECT * from issued where issued.Date >='" +from + " ' And issued.Date < '" + to + " ' "; //مقارنة الاسم
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
item = new ListViewItem(dr[0].ToString());
item.SubItems.Add(dr[4].ToString());
f6.listView1.Items.Add(item);
x += Convert.ToInt16(dr[4]);
}
}
Help me please..
As Marc stated above, you really should be careful of sql injection attacks here. Look it up and do some research because you'll suddenly find you have a compromised server someday.
Is your Date column of type Date or DateTime? If it's DateTime, then simply comparing on a specific date does not include that date for the upper bound. For instance, if your dates are:
From: 1/1/2013
To: 1/1/2014
Then your comparison would not return any values from 1/1/2014, and the last date included in the search would actually be 12/31/2013.
Add a little more specifics to your question regarding what values you are actually sending in your sql and what the types are in your db and you'll get a better answer!
Good luck!
I want to retrieve records which the users are active n last modified date is greater than lastrun date, i wrote the following CAML but doesnt seem like working. Any help is much appreciated.
camlQuery.ViewXml
= "<View><Query><Where><And><Eq><FieldRef Name='Active'/>"
+ "<Value Type='Boolean'> " + 1 + "</Value></Eq><Gt>"
+ "<FieldRef Name='_DCDateModified'/><Value Type='DateTime'>"
+ lastUpdate + "</Value></Gt></And></Where></Query></View>";
I suppose it could be a Date formatting issue. You could attempt to:
1) explicit exclude of Time portion of DateTime
2) convert in advance your Date
like this:
...<Value IncludeTimeValue='False' Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(lastUpdate) +"</Value>
I need a statement to select data from an MS Access database table.
WITHIN selected dates
I have two textboxes in my GUI called StartDate and EndDate
I want to select data within those 2 dates.
I have tried 2 methods.
The first is
" DAY(V.RegDate) between " + Start.ToString("dd")
+ " and " + End.ToString("dd");
" and MONTH(V.RegDate) between " + Start.ToString("MM") + " and "
+ End.ToString("MM");
" and YEAR(V.RegDate) between " + Start.ToString("yyyy") + " and "
+ End.ToString("yyyy");
V.RegDate is the date column from the database.
But it returns me no data when I select 01/08/2010 and 01/09/2010 while there is some data at 25/08/2010.
I think that is because I chose the date separately and since the 2 dates are the same,
returns me nothing
I have tried another way...
" V.RegDate between #" + Start.ToString("dd/MM/yyyy") + "# and #" _
+ End.ToString("dd/MM/yyyy") + "#";
This also return me nothing
Any Ideas????
This pattern works for me:
SELECT sometable.somedate
FROM sometable
WHERE (((sometable.somedate) Between #2/1/2010# And #4/1/2010#));
(in this case it's MDY, I normally prefer ISO-ish style - YYYY/MM/DD because there is no way that Access can screw that up)
Maybe you've got your date set using the American default - MDY instead of the British(?) standard DMY.