Select datetime column from SQL Server 2008 R2 table - c#

I have a DateTime column on a table in SQL Server 2008 R2 database. My c# front end is inserting a row in this table with a datetime parameter.
DateTime T = DateTime.Now;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "cargridsnapshot";
cmd.Parameters.AddWithValue("#t", T);
cmd.ExecuteNonQuery();
The datetime column in the row in the table is this:
2013-09-04 16:21:23.450
but
select * from table
where TIMECOLUMN = '2013-09-04 16:21:23.450'
returns no results.

SQL Server can represent datetime in a variety of ways when comparing a string to your database value. Some have the month before the day, some day before month. The localization of your SQL Server is causing your issue; you can read more about it here. Specifically, look for SET DATEFORMAT.

SQL Server only stores time to approximately 1/300th of a second-you are probably using a value that cannot accurately be represented with the available bits.
Edit
You can check this behavior with the following code:
select CONVERT(DATETIME, '2013-09-04 16:21:23.450') -- returns 2013-09-04 16:21:23.450
select CONVERT(DATETIME, '2013-09-04 16:21:23.451') -- returns 2013-09-04 16:21:23.450
select CONVERT(DATETIME, '2013-09-04 16:21:23.452') -- returns 2013-09-04 16:21:23.453
select CONVERT(DATETIME, '2013-09-04 16:21:23.453') -- returns 2013-09-04 16:21:23.453
select CONVERT(DATETIME, '2013-09-04 16:21:23.454') -- returns 2013-09-04 16:21:23.453
select CONVERT(DATETIME, '2013-09-04 16:21:23.455') -- returns 2013-09-04 16:21:23.457
Bottom line
Don't try to identify rows with a datetime...

What if you use this
Select * from table where convert(date,yourcolumn)=convert(date,your_datetime)
but kindly note it will compare on date only

The SQL Server datetime type is accurate only only to increments of .000, .003, or .007 seconds.
So when you save a value with .450, that will be fine. But if you saved with .451, that would get rounded back down to .450, and then it wouldn't match up.
You can avoid this by using a datetime2 column type.
Also - you probably shouldn't be storing DateTime.Now in your database. Read here.

Related

Fetching date in SQL Server 2008

How to split date in select query of SQL Server 2008 ?
If you feed you feed current date in database and the type of that column in database is date time then It will store the date as well as time.If any one want to fetch only the date part from that column then what can he do.
I can use this query but it should not work.
SELECT DonorName, DATE(DateOfDonation) AS DateOfDonation
FROM CreateDonorDetail;
it give error-
'DATE' is not a recognized built-in function name.
SQL Server 2008 has a DATE datatype - but it's disabled if your database was upgraded from a SQL Server 2005, and you didn't change the database compatibility level.
Check your compatibility level like this:
SELECT name, compatibility_level
FROM sys.databases
WHERE name = '-Your-database-name-here-'
If this compatibility level is 90, it's set to SQL Server 2005 and you won't be able to use DATE. Update your compatibility level to 100:
ALTER DATABASE AdventureWorks2012
SET COMPATIBILITY_LEVEL = 100
Now you should be able to use DATE:
SELECT
DonorName,
CAST(DateOfDonation AS DATE) AS DateOfDonation
FROM CreateDonorDetail;
Cast as Date type:
SELECT DonorName, Cast(DateOfDonation as Date) AS DateOfDonation
FROM CreateDonorDetail;
Prior to SQL Server 2008 you colud use something like this:
SELECT DonorName, CAST(FLOOR(CAST(DateOfDonation AS FLOAT)) AS datetime) AS DateOfDonation FROM CreateDonorDetail
DECLARE #DateTime DATETIME
SELECT #DateTime = '2010/05/20 11:21:13'
SELECT CONVERT(VARCHAR(10),#DateTime ,105) AS Date
the result will be in dd-mm-yyyy format (105)
Hope it will helps
mark as answer if it helped

SQL query returning timestamp attached with a date only column

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");

How to get the latest record from a database in ASP.Net using C#?

I want to get the latest record from my database
Below is my query
select * from tablename order by date desc, time desc
My Issues
My query show uncertain behavior, sometimes it works properly in website but sometimes is does not give the desired result. It happen because of time part
Example
2:00 pm and 2:00 AM how to compare this time?
The exact Query is for getting very latest row
select Top 1 * from tablename order by date desc, time desc
so if you´re datatype is really VARCHAR you can convert it to date and time in the order by clause
begin
declare #time1 time
declare #time2 time
declare #test table(
mytime varchar(max),
mydate varchar(max)
)
insert into #test VALUES('2:00 pm', '10-10-2010')
insert into #test VALUES('2:00 am', '08-05-2009')
SELECT * FROM #test ORDER BY CONVERT(date, mydate) DESC, CONVERT(time, mytime) DESC
end
but i would suggest to change your table-structure, to have only one DateTime Column!
Or at least change the DataType to TIME and DATE!
Unless you have some special requirement, it will be better to use only one column of datetime type to store date and time.
If two columns are must, you can store time in 24-hour format.
Try this. Convert the date and time into a datetime and then order by that.
select *
from tablename
order by cast(date AS DATETIME) + cast(time AS DATETIME)
Select Top 1 ColumnName from dbo.TableName Order by date,time desc
I think u should convert the time in AM/PM to 24-hrs format. Check this out http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

Comparing current date in stored procedure

I am trying to check in a stored procedure a date in table if it is equal to Today's date.
Code is
DECLARE #p0 datetime
Set #p0 =GETDATE()
Select * from testtable
where dateCol=#p0
This doesn't work it just gives empty rows. How can I accomplish that? Thanks
If dateCol is just the date, not DATETIME, you can use:
SELECT *
FROM Table
WHERE dateCol = CAST(GETDATE() AS DATE)
You need:
SET #p0 = CONVERT(CHAR(10),GETDATE(),103)
GETDATE() returns the date and time. You probably have records with todays date but not at this exact time.
Also you don't really need to store it in #p0. You could use the expression directly in the WHERE clause.

How to select only date part from SQL Server CE datetime column?

I am using SQL Server CE database and C# language. I have a table with a datetime type column. I want to use a SELECT statement like
SELECT * FROM Data WHERE Date = #Date
But I need only date part of the datetime value. So I can add parameter like
SQLCmd.Parameters.Add("#Date", date.Date);
My problem is with SELECT statement I think.
Instead of that you can use DateDIFF
SELECT * FROM Data WHERE DATEDIFF(day, Date, #Date)=0
SELECT * FROM Data WHERE DAY(Date) = DAY(#Date)

Categories

Resources