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)
Related
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
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");
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.
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.
What is the best way to extract date from SQL Datetime field in C#.Net?
If you're not using an ORM mapper / framework that is automatically translating the date-time formats, based on the database server's configuration, I'll assume that you're trying to do this manually. Also assuming you're using SQL Server.
Its not ideal, but better to make sure that you are getting the datetime out of the database in a known format (your database server might be configured for a different locale to your web/app server - for whatever reason)
Have a look at this article for the SQL Convert functionality:
CONVERT(data_type,expression,date Format style)
USA mm/dd/yy - select convert(varchar, getdate(), 1)
ANSI yy.mm.dd - select convert(varchar, getdate(), 2)
British/French dd/mm/yy - select convert(varchar, getdate(), 3)
... etc.
Once you've got your query data back, using the Convert static class:
Convert.ToDateTime(reader["DateColumn"])
SqlCommand cmd = new SqlCommand("SELECT BeginDate FROM table_name ");
DateTime beginDate = (DateTime)cmd.ExecuteScalar();
Using a DataReader there are several ways:
reader.GetDateTime(ColumnNumber)
(DateTime)reader[ColumnNumber]
Convert.ToDateTime(reader[ColumnName])