[Solved] SQL Server Char/VarChar DateTime Error [closed]


This should look more like this:

DateTime now = DateTime.Now;
DateTime after1month = DateTime.Now.AddMonths(1);

SqlCommand cmd = new SqlCommand("SELECT * FROM TABLE WHERE THEDATE BETWEEN @now AND @after1month", connection);
cmd.Parameters.Add(new SqlParameter("@now", System.Data.SqlDbType.DateTime).Value = now);
cmd.Parameters.Add(new SqlParameter("@after1month", System.Data.SqlDbType.DateTime).Value = after1month);

Sometimes you can do it directly on SQL Server side using query:

SELECT * FROM TABLE WHERE THEDATE BETWEEN getdate() AND dateadd(mm, 1, getdate())

without parameters from .NET.

Added (after comment):

dateadd is a SQL Server function that allows to add a specyfic interval to date and returns it.

In this case dateadd(mm, 1, getdate()) adds one (1) month (mm) to current datetime (getdate()). More info on datepart identifiers and function itself on MSDN dateadd.

2

solved SQL Server Char/VarChar DateTime Error [closed]