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
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'm trying to take a 'calendar.selecteddate' from a web form calendar, and pass that date to my sql database date data-type column.
If I select the 5th of January 2014 the entry works fine (2014/05/01). If I select 13th January there is an exception while sending the data to the database. It's converting the date to a yyyy/mm/dd format. Is there a way to change either my sql database data type format - or, a nice bit of code that'll convert it before I pass it to the database?
SqlHandler.SqlQuery(String.Format("INSERT INTO Sessions VALUES ('{0}', '0')"
, Calendar.SelectedDate.ToString()));
thanks as always
I store datetime values in the database as sql float type (Converted from an DateTime.OADate) for a myriad of reasons however in certain circumstances it is nice to get a human readable date/time column back from the database. I have found that I can execute the statement
SELECT CAST (timerecorded_utc as DATETIME) FROM tablename
and it will give me the date time string I am looking for but it seems to be off by exactly 2 days. I realize I can just modify the statement (since in time represented as a double 1 day = 1.0) to be
SELECT CAST (timerecorded_utc-2.0 as DATETIME) FROM tablename
BUT I was wondering if this is consistent AND it seems to me there is some reason for the discrepancy that I am missing.
It's because the epochs the dates use are different.
SQL Server's DATETIME uses 01/01/1900 00:00:00 as the epoch, which you can see by running the following query: SELECT CAST(0 AS DATETIME)
OADate is a bit odd, as it could have an epoch of 30/12/1899 00:00:00 or 31/12/1899 00:00:00 depending on whether you believe the Visual Basic or Excel guys, respectively. It would appear that from your two day difference, the .NET version goes with the 30th.
So, epoch off by two days gives two days difference in the outcome when you convert between the two types of date via a raw number.
Epic Epochs... Here is my TSQL solution in SQL Server using the built in
"DateAdd" function:
Select DateAdd(DAY, cast([ENDING DATE] as decimal(10,0)), '12/30/1899')
from YourTable
In my case I was importing a string saved in Excel via C# Core App and uploading to a SQL Server database so my [Ending Date] is a string which I casted as a decimal with no precision as I only needed the actual date, and not the time of day. As #GregBeech mentioned, your base date might be '12/31/1899'.
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))
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])