MS Access / Excel SQL Server query producing Conversion error DATETIME - sql-server

I've looked around for a solution for this, but none of them seem to fix my problem
SELECT TOP 10 [Appointment_Date]
FROM dbo.RF_Performance_Referrals_Main
WHERE (([Appointment_Date]) < '7/21/2014')
ORDER BY [Appointment_Date] DESC
Above is the Simplified version of the query I am trying to. I keep get an error
[Microsoft][ODBC SQL Server Driver][SQL Server]Then Conversion of a
varchar datatype to a datetime data type resulted in an out of range
value
I thought maybe there is a problem with my connection...
so I tried it on Excel and got the exact same error...
I checked the SQL Server table that particular field is set to DATETIME
So why is it giving an error?
I tried all sorts of stuff on Cast/Convert, but none of them seem to work i still get the same error. I don't really get why.
Please help if fix this.
Thanks a lot in advance

SQL Server's default date time syntax is YYYY-MM-DD HH:MM:SS as in (1900-01-01 00:00:00)
You either have to cast your string to a date or reformat your input to the expected datetime defaults:
http://msdn.microsoft.com/en-us/library/ms187819.aspx: Assuming US Local and standard defaults.

Change to
SELECT TOP 10 [Appointment_Date]
FROM dbo.RF_Performance_Referrals_Main
WHERE (([Appointment_Date]) < #2014/07/21#)
ORDER BY [Appointment_Date] DESC

Related

Blank dates coming up as weird dates instead of NULL in SQL

I am importing data from postgres into sql server. Fields where data type is 'timestamp without (or with) time zone' i used datetimeoffset in sql table but couldn't create package with the same. So i changed the datatype as datetime.
Now, when I import data,all the dates and time are coming perfectly alright but where there is no data ( blank) then its displayed as some weird date instead of NULL in my SQL table.
Those weird dates are -
1899-12-30 00:00:00.000
1753-01-01 00:00:00.000
What am i missing and how to resolve this?
Maybe that's a simple thing but I am quite new to SQL so apologies if its really easy but any help is appreciated.
Thanks,
AP
Suppose your meaningful data is from 2000 onwards, then run a update statement like:
update table
set column = NULL
where column < cast('01-01-2000' as datetime)

SQL Server Incorrectly Parsing Date

SQL Server 2005
SELECT TOP 10 * FROM abc WHERE aDate = '2014-01-20'
When querying the above in SSMS it would normally return results where aDate is 20 January 2014. However for another user on the same server, it returns a date conversion error and only works when running the following query:
SELECT TOP 10 * FROM abc WHERE aDate = '2014-20-01'
I've checked regional language settings on the local machine and it's exactly the same as mine. Any ideas welcomed.
It is not the regional language settings on the machine that count in this case but the one defined on the database's options.
Anyway, to avoid having to rely on the regional language settings when parsing datetime in queries I would encourage you to use an invariant ISO date format : {d 'yyyy-MM-dd'}. Note there is also one for specifying the hours (ts).
It was account specific, the setting was stored as 'British - English' as opposed to 'English'. Changing this to 'English' resolved the problem. Thank you for your responses.
This error occurred as the SQL server tries parse the date value 20 as month and it causes error as 20 is not a valid month .Always It is good practice to use the date format 'dd-MMM-yyyy' which will work with any type of SQL COLLATION and regional language settings.

Exception when using TSQL type "Date" with Mono

I'm using Mono 2.10.8.1 on Ubuntu 12.04 Server.
I'm using an ADO.net TableAdapter to grab data from SQL Server 2008. When I encounter a Date column, Mono gives the following error:
No mapping exists from SqlDbType Date to a known DbType.
I'm not entirely sure what Mono uses for DB access (FreeTDS/etc) so I'm not 100% sure where to even start my search for a solution.
An obvious solution would be to simply change the column in the DB to DateTime, but since it is in production I do not have that option.
Has anybody else encountered this error before?
Thanks
In your TableAdapter SQL statement, try casting or converting the field being returned as a date to a datetime or, if necessary, to a varchar field with the necessary formatting. You can achieve this by doing the following:
Select field1
, field2
, CAST(date_field as datetime) as New_datetime_field
, CONVERT(varchar(10),date_field,101) as New_varchar_field --stored as MM/DD/YYYY string
From Table
Doing this will now cause the field returned by the query or stored procedure to be recognized as SQL as a datetime or varchar field. It should then be passed on using the TableAdapter as a datetime or varchar field. Using the convert statement, you can convert a date into any number of formats (see here for more information).

SQL Date Type Conversion Error

I get the date like this
DateTimePicker1.Value.Date.ToShortDateString()
and try to insert to my database.
The error is :
Conversion failed when converting date and/or time from character string.
It was working yesterday. Today I delete all values in my table and is not working now
It looks like your app server and db server have different regional date setting. Also, DateTimePicker1.Value.Date yields a DateTime and you should use that to add it into the db without having to go through the regional conversion mess.
Get your date using:
CDate(DateTimePicker1.Value.Date.ToShortDateString())

order by DATEDIFF gives Conversion failed error

I'm editing a query i build in ms sql.
I need to order the items on date, but that gives an error.
locations_aanvang gives 2012-08-12(yyyy-mm-dd). so i extend it to 2012-08-12 00:00:00 +1:00 for EST time.
So to order the i need to convert it to unix timestamp (right?).
The query is:
SELECT TOP 6 * FROM jd_lighthouses
WHERE locations_aanvang != ''
ORDER BY (SELECT DATEDIFF(s, '1970-01-01', locations_aanvang+' 00:00:00 +1:00')) DESC
And the error i get is:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Conversion failed when converting date and/or time from character string.
What am i doing wrong? and can i do this easier?
Thanks in advance!
Kind regards,
Bram Hammer
Because we can'r clarify to question, only help is that:
if locations_aanvang is character type then you can avoid error changing code like this:
DATEDIFF(s, '1970-01-01', locations_aanvang + ' 00:00:00')
Since you use SQL Server Native Client 10.0 i assume you have SQL Server 2008 and DATE datatype. To get newest locations use:
ORDER BY CAST(locations_aanvang as DATE) DESC
Hmmm. Let me take a stab.
I think MS SQL warns against sorting by a column/calculation that
isn't directly in return results of your SQL. This is definitely so
in Oracle.
I'm assumming you can plug in a literal value for your
variable and it runs just fine?

Resources