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])
Related
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.
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
How to query the rows based on DateTime in SQL Server 2005 Express installed on windows server 2008 r2?
I have code that selects rows based on from and to date values. It's working in my Windows 7 system . But, It's not working on Windows Server 2008 R2 ... Any help
this.filterreportTableAdapter.FillBy(this.cRdataset.filterreport_datatable, fromdate, todate);
and my SQL query is
SELECT *
FROM tablename
WHERE DATE BETWEEN #fromdate and #todate
It sounds like date/time values are being passed as strings. This is always a bad idea, and formatting issues abound. The best approach here is simply: use correctly-typed parameters. if both client and server know it is a DateTime, then it is passed as a primitive vaue, not a string - thus no formatting problems.
Make sure that fromdate and todate in the c# are DateTime, and that #fromdate, #todate and tablename.DATE in the TSQL are datetime.
Please set the datetime format same as the one on your windows 7 e.g. : dd/MM/yyyy
it seems that you have a date format mistake when passing date parameters.
there are two solutions:
first, you can try to use date format that works everywhere that is:
YYYYMMDD ex : 20120722.
another solution is to make a DateDiff in sql query
Best regard
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)
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))