dd/mm/yyyy

G

GM

i have datacolumn that formating dd/mm/yyyy hh/mm/ss. i try getting between from two date like
select * from X Where Date between 01.11.2008 11/12/33 and 30.11.2008 11/12/33. but it runs wrong.i can not displaY DIFFRENCE DATE
 
J

jp2msft

To expand on Pete's answer: The format of the date you are searching on does
not match what the server is using.

A better fix for this is to use parameters in your SQL statement and pass an
actual DateTime control.

For example: Say your SQL statement looked like this:

string sqlStr = "SELECT * FROM X WHERE Date BETWEEN @Date1 AND @Date2";

(if the SQL doesn't work, I apologize)

using (SqlDataAdapter da = new SqlDataAdapter(sqlStr, m_sqlConnectionStr))
{
da.Parameters.Add("@Date1", DateTime).Value = m_objDate1;
da.Parameters.Add("@Date2", DateTime).Value = m_objDate1.AddDays(30);
DataTable table = new DataTable();
try {
da.Fill(table);
} catch (SqlException err) {
#if DEBUG
MessageBox.Show(err.ToString());
#else
Console.WriteLine(err.ToString());
#endif
}
if (0 < table.Rows.Count) {
foreach (DataRow r in table.Rows) {
// do something with your row "r"
}
}
}
 
G

Göran Andersson

GM said:
i have datacolumn that formating dd/mm/yyyy hh/mm/ss. i try getting between from two date like
select * from X Where Date between 01.11.2008 11/12/33 and 30.11.2008 11/12/33. but it runs wrong.i can not displaY DIFFRENCE DATE

Are you storing the values as varchar in the database? A datetime value
doesn's have a format at all, it only gets a format when you format the
value into a string.

If you store the values as varchar, they are not in a format that is
comparable. The only comparable date format is the ISO 8601 format, i.e.
yyyy-mm-dd hh:mm:ss, as the components are always the same length, and
it places the most significant components first in the string. To
compare dates in any other format, you have to convert each value into a
datetime value before doing the comparison, which gets rather slow.

If you store the values as datetime, and see them formatted like you
described in a program that manages the database, e.g. Management
Studio, that only means that the program displays the dates that way,
not that the database itself will understand that date format. The best
way to send DateTime values to the database is to use parameters, as
jp2msft described.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top