string query = "Select *From myTable where myTime>datetime('now','localtime','" + (-1 * day).ToString() + " days') ORDER BY myTime DESC";
day is a integer variable. If I set the day value to 1, database is filtering for 24 hours.
But if I set the day value to 1, I only want to see the logs of the new day.
How can I do that? Thank you for your help.
string query = "Select *From myTable where myTime>datetime('now','localtime','start of day', '" + (-1 * day).ToString() + " days') ORDER BY myTime DESC";
Related
I have 3 tables: suppliers, bills, and payments. Each supplier has payments and bills. I want to retrieve a report like this one:
Supplier Name | total bills | total payments | balance
For a specific period that is specified by 2 DateTimePickers.
This my SQL query:
OleDbCommand cmd = new OleDbCommand("select c.sup_Name,c.sup_Place,sum(a.bill_Total),sum(d.pa_Value)
from (suppliers c left JOIN bills a
on c.sup_Id = a.bill_From)
left join payments d on c.sup_Id = d.pa_To
where (a.bill_Date >= '" + txbFrom.Text + "' and a.bill_Date <= '" + txbTo.Text + "') and (d.pa_EntryDate >= '" + txbFrom.Text + "' and d.pa_EntryDate <= '" + txbTo.Text + "')
group by c.sup_Name,c.sup_Place order by c.sup_Name asc", objConn);
OleDbDataReader dataReader = cmd.ExecuteReader();// Here the error appear.
For MySQL I had to convert the date: dt = DateTime which can easily be extracted from the DateTimePicker.
dt.ToString("yyyy-MM-dd H:mm:ss");
Don't format the parameters into an SQL string: instead use OleDbParameter to do this for you (and help avoid SQL injection attacks).
https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter(v=vs.110).aspx
I'm working on a C# program. In that program, I select some dates from Access 2010 database and process data. But, I can't access and process data on rows containing dates from a range of 01/12/2014 and 11/12/2014 but I can process remaining dates.
cmd = new OleDbCommand("SELECT MIN([Vch No]) FROM [" + group + "] WHERE [Entry Date]=#" + date + "#", conn);
long Vch = long.Parse(cmd.ExecuteScalar().ToString()) - 1;
I get error on line long Vch = ... - 1; saying can't get proper Input string. Whereas, it works for other dates either before or after these particular dates.
I declare the date by this code :
cmd = new OleDbCommand("SELECT TOP 1 * FROM (SELECT DISTINCT [Entry Date] FROM [" + group + "]) WHERE [Entry Date]>#" + initDate + "# ORDER BY [Entry Date]", conn);
dr = cmd.ExecuteReader(); dr.Read();
string date = dr["Entry Date"].ToString();
Thank you in advance!
Try setting you date variable to a non culture specific date format, so YYYY/MM/DD e.g. 2014/12/01
If date is a C# DateTime object, you'll need to call:
date.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
As per convert datetime to yyyy/MM/dd
So:
cmd = new OleDbCommand("SELECT MIN([Vch No]) FROM [" + group + "] WHERE [Entry Date]=#" +
date.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture) +
"#", conn);
long Vch = long.Parse(cmd.ExecuteScalar().ToString()) - 1;
EDIT TO ADD
As per #Gord-Thompson's comment, it's always considered best practise to use parameters where ever possible (generally always!).
So:
cmd = new OleDbCommand("SELECT MIN([Vch No]) FROM [#pGroup] WHERE [Entry Date]=#pDate", conn);
cmd.Parameters.Add("#pGroup", OleDbType.NVarChar).Value = group;
cmd.Parameters.Add("#pDate", OleDbType.DateTime).Value = date;
long Vch = long.Parse(cmd.ExecuteScalar().ToString()) - 1;
Try to avoid using cmd.Parameters.AddWithValue() - see http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ for more details.
I am developing a windows form application in C#. I have a query where i want to select from date to date. the result is always 0.
My DB is a .sdf file, and in my table I have a datetime field to store the order date as DateTime.Now, and my PC dateformat is 10.12.2014,
Can any one please help me and explain why is this hapening..! Is there any better file format to save locally.? the application will be installed on several PC's, will the datetime format be a problem .? your help is much appricaitaed.. check please provide with some code.
internal static DataTable SearchItemsInOrders(string searchWord, string table, int minValue, int maxValue, out DataTable dtTable, DateTime dateFrom, DateTime dateTo)
{
string commandText = "select A.ItemName, B.Site, B.OrderId, A.Qty, B.Requester, B.Receiver, B.Date ";
commandText += "From tblOrderLine AS A Inner join tblOrder As B on A.OrderId=B.OrderId ";
commandText += "where A.ItemName='" + searchWord + "' and B.Date >= '" + dateFrom.ToShortDateString() + "' and B.Date <='" + dateTo.ToShortDateString() + "' Order By A.OrderId DESC";
SqlCeDataAdapter adp = new SqlCeDataAdapter();
SqlCeConnection con = ConAndData.Con;
dtTable = new DataTable();
adp = new SqlCeDataAdapter(commandText, con);
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
adp.Fill(minValue, maxValue, dtTable);
return dtTable;
}
Try using this query,
string commandText = "select A.ItemName, B.Site, B.OrderId, A.Qty, B.Requester, B.Receiver, B.Date ";
commandText += "From tblOrderLine AS A Inner join tblOrder As B on A.OrderId=B.OrderId ";
commandText += "where A.ItemName='" + searchWord + "' and CONVERT(NVARCHAR(10),B.Date,121) >= CONVERT(NVARCHAR(10),'" + dateFrom.ToString("yyyy-MM-dd") + "',121) and CONVERT(NVARCHAR(10),B.Date,121) <= CONVERT(NVARCHAR(10),'" + dateTo.ToString("yyyy-MM-dd") + "',121) Order By A.OrderId DESC";
Hope this helps...
As it was suggested in comments - you have to use parameterized queries.
So, your commandText should be like:
string commandText = "select A.ItemName, B.Site, B.OrderId, A.Qty, B.Requester, B.Receiver, B.Date ";
commandText += "From tblOrderLine AS A Inner join tblOrder As B on A.OrderId=B.OrderId ";
commandText += "where A.ItemName=#ItemName and B.Date >= #DateFrom and B.Date <=#DateTo Order By A.OrderId DESC";
and later you should add parameters and values to the command:
adp = new SqlCeDataAdapter(commandText, con);
adp.SelectCommand.Parameters.Add("#ItemName", SqlDbType.NVarChar).Value = searchWord;
adp.SelectCommand.Parameters.Add("#DateFrom", SqlDbType.DateTime).Value = dateFrom;
adp.SelectCommand.Parameters.Add("#DateTo", SqlDbType.DateTime).Value = dateTo;
This also will prevent possible problems caused by incorrect date format produced by ToShortDateString().
I am trying to figure out the way to sum values of certain columns. How do I get Date month value from Date column where all values are in date (yyyy/mm/dd) format?
What I should write in :
WHERE Data = '"?????"' ";
I want to pick up date where month equals 12.
Here is my code :
MySqlConnection cnn = new MySqlConnection(connectionString);
cnn.Open();
string query = "select sum(SUMA) from `nuolatines pajamos` WHERE ID = '" + perdavimo1.id_permetejas.ToString() + "' WHERE Data = '"+ now.Month +"' ";
MySqlCommand createCommand = new MySqlCommand(query, cnn);
var sum = createCommand.ExecuteScalar().ToString();
nl_pajamos.Text = sum.ToString();
DateTime.Parse(stringobject,cultureinfo);
for example
DateTime.Parse("20-01-2014",new CultureInfo("nl-BE
I have a database ShiftChange, and the fields are
Fromdate='09/11/2014', Todate='11/11/2014',Shift='MG' and Month='11'(dd/MM/YYYY format)
Fromdate='11/11/2014', Todate='15/11/2014',Shift='AF' and Month='11
I want to find today's shift from these table. I tried like this,
DateTime today = DateTime.Today; // As DateTime
string s_today = today.ToString("dd/MM/yyyy");//system date converted to given format
int month = DateTime.Now.Month;
string str = "Select Min(Fromdate), Max(Todate) From ShiftChange where Month='" + month + "'";
SqlDataReader dr = conn.query(str);
if (dr.Read())
{
string mindate = dr[0].ToString();
string maxdate = dr[1].ToString();
string str3 = "select Shift from ShiftChange where '" + s_today + "' >= '" + mindate + "' and '" + s_today + "' <= '" + maxdate + "' and Month='"+month+"'";//checks current shift type of selected date.
SqlDataReader dr3 = conn.query(str3);
if (dr3.Read())
{
string shiftid = dr3[0].ToString();
}
Here connection is my connection class and query is my (sqldatareader dr) method.When I run this query I am getting shiftName 'MG' but actually the shiftName correspondent to today's date is 'AF' but it not getting. I hope it is my query's issue. Please show me the error
First of all you should make your query parameterized or you can prepare sql stored procedure and use that stored procedure in your code. I am giving you sql code for stored procedure here.
declare #month varchar(50) = '11'
declare #mindate varchar(50)
declare #maxdate varchar(50)
declare #date varchar(50)
select #mindate=min(fromdate),#maxdate = max(todate)
from ShiftChange Where Month = #month
select TOP 1 Shift From ShiftChange
WHERE #date>= fromdate and todate<=#date
and month = #month
order by todate desc
Btw, it's always better to use datatype date or datetime when dealing with dates.
I solved this issue with #Mukund's help. I applied his code in my project like this,
DateTime today = DateTime.Today; // As DateTime
string s_today = today.ToString("dd/MM/yyyy");
int month = DateTime.Now.Month;
string str = "Select Min(Fromdate), Max(Todate) From ShiftChange where Month='" + month + "'";
SqlDataReader dr = conn.query(str);
if (dr.Read())
{
string mindate = dr[0].ToString();
string maxdate = dr[1].ToString();
string str3 = "select TOP 1 Shift From ShiftChange WHERE '"+s_today+"'>=Fromdate and Todate>='"+s_today+"' and month = '"+month+"' order by Todate desc";
SqlDataReader dr3=conn.query(str3);
if(dr3.Read())
{
string shift = dr3[0].ToString();
}