I've got to make a query from C# and compare with a datetime. Datetime is stored in my database like this:
2014-11-09 00:00:01
and I'm using this query:
SELECT *
FROM Table
WHERE DATETIMEVAR = '19/11/2014 0:00:01' AND OTHERVAR = 1
But it's not in the same format. Is there a way to convert from System.DateTime in C# to dates in SQL Server, or a way to cast from SQL Server the datetime in that format?? Should I check with like instead of = in Where clause.
Thanks.
The best way would be to have a parametrized query in C#, and then set your date time parameter as a datetime (instead of relying on converting dates back and forth to and from strings).
So you should have something like:
SELECT *
FROM Table
WHERE DATETIMEVAR = #DateTimeValue AND OTHERVAR = 1
and then define that #DateTimeValue parameter as datetime and set it as datetime
That approach:
prevents any SQL injection vulnerability
avoid any string/formatting issues - it compares a DATETIME to a System.DateTime in their native format
If you are generating the query from a DateTime object, just do date.ToString("yyyy-MM-dd HH:mm:ss") to convert it to that format in your query.
Related
I am working on a website in asp.net. I am getting a date from a web page and then depending on the user input I want to get results from SQL Server database (using stored procedures).
Problem is that I am getting date only from UI in this format 2016-10-08 which is of type string. But in the database, I have a column which is of type datetime in this format 2016-10-08 17:38:00.000.
I am using this query to search but it does not work.
select *
from table
where acceptedDate like #sDate+ '%';
where sDate is input parameter for stored procedure. Please help. thanks
Don't pass dates as strings. Pass them as DateTime.
The .Net DateTime maps directly to SQL Server's DateTime. All you have to do is parse the string to a DateTime struct in your .Net code and pass it as a parameter to your stored procedure.
To search for a specific date and ignore the Time portion of the DateTime, better use >= and < in your sql:
select *
from table
where acceptedDate >= #Date
AND acceptedDate < DATEADD(DAY, 1, #Date);
If you only want compare with day level and ignoring the hours part, you can use DateDiff function.
Pass d or DAY to interval parameter of DateDiff
For example:
DECLARE #sDate VARCHAR(100)='2016-10-08'
IF ISDATE(#sDate)=1
BEGIN
select *
from table
where datediff(d,acceptedDate,#sDate)=0 --same day
END
ELSE
PRINT 'Invalid date format!'
I am trying to get date from a column which has its datatype set as date and in SQL Server the column also contains only date but when i run query from my application C#:
resultData[i][3] = sqlRdr["addedOn"].ToString();
It gives me a default time signature attached to it
select addedOn from sitrep;
I also tried using below cast but still same results:
select Convert(Date, Convert(datetime, addedOn)) as addedOn from sitrep;
If your saving it to a string i would try.
Convert.ToDateTime(sqlRdr["addedOn"]).ToShortDateString();
You can modify your query as:
resultData[i][3] = sqlRdr["addedOn"].ToString("M/d/yyyy");
Here's my query:
string sql = #"INSERT INTO MY_TABLE(DATE)
VALUES (convert(datetime, '{0}')";
sql = string.Format(sql, myDate);
myDate is a C# DateTime object and has this value before the string.format:
MY_TABLE has a DATE column as a DateTime type.
I got this error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I need to store the myDatevalue in a format like: 31/12/2013 23:59:00 into MY_TABLE.
better to use sql parameters
string sql = #"INSERT INTO MY_TABLE(DATE)
VALUES (#DATE)";
add the parameter to your SqlCommand with SqlParameterCollection.AddWithValue Method
command.Parameters.AddWithValue("#DATE", myDate);
OR, with SqlParameterCollection.Add Method
command.Parameters.Add("#DATE",SqlDbType.DateTime).Value = myDate
DateTime.Now.ToString("dd/MM/yyyy H:mm:ss")
For a list of the parameters see MSDN. (Scroll down a bit to see the table.)
(And it is advised to use Parameters.AddWithValue to avoid SQL injection etc.)
If I have a C# DateTime object and a column in SQL Server of type Date (not DateTime)
For a stored procedure that takes in the date time from C# do I have any option other than passing in the C# DateTime as a SQL Server DateTime or can I pass it in of type Date?
Define it as a DATE in your stored procedure
CREATE PROCEDURE dbo.YourProcedure (#FromDate DATE)
.....
and call it like this from C#:
yourSqlCmd.Parameters.Add("#FromDate", SqlDbType.Date);
yourSqlCmd.Parameters["#FromDate"].Value = yourDotNetDateTime.Date;
You could always use the DateTime.Date Property
Gets the date component of this instance.
I need your help in small problem, I have a column (data type timestamp) in SQL Server 2008.
Now I want to show this timestamp value in ASP.Net C# app as string. Is there any way to do that?
I tried it using regular data fetching in ASP.Net but it produced System.byte[] as output rather than actual value. In SQL Server Management Studio values are represented as 0x000000000000B3C0.
One option is to change it to the Date, while getting from the database. Like:
SELECT timestamp = DATEDIFF(s, '19700101', yourTimestampColumn)
FROM yourTable
I don't know if i catch you, but in sql you can cast timestamp value to datetime then to varchar like this:
declare #val timestamp = 0x0000AAE200B29565
select cast(cast(#val as datetime) as varchar(max))