Currently using SQL Server 2008. In an effort to debug some bad date data being processed, the following code was written with an example of the bad data.
SELECT ISDATE('10-22-002')
SELECT YEAR('10-22-002')
Running the statements on database A, the results are: '1' and '2002'.
Running the statements on database B, the results are: '1' and an error.
The date format is MDY on all sessions before running the statements.
Msg 241, Level 16, State 1
Conversion failed when converting datetime from character string.
Everything I'm able to find says the date format is set at either server or session level. Is there a setting at the DB level for this?
You need to cast '10-22-002' as a datetime.
SELECT YEAR(cast('10-22-002' as datetime))
Related
I am trying to do a simple BULK INSERT from a large CSV file to a table. The table and the file have matching columns. This is my code:
BULK INSERT myTable
FROM 'G:\Tests\mySource.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
-- ROWTERMINATOR = '0x0a',
BATCHSIZE = 1000,
MAXERRORS = 2
)
GO
As you can see I have tried with row terminators \n and 0x0a (and a bunch more)
I keep getting a type mismatch error:
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 18 (createdAt).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 18 (createdAt).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 4, column 18 (createdAt).
Msg 4865, Level 16, State 1, Line 1
Cannot bulk load because the maximum number of errors (2) was exceeded.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Column createdAt is of type datetime:
CREATE TABLE [dbo].[myTable]
(
...
[createdAt] [datetime] NULL,
...
)
These are the values of the createdAt column as taken from the first three rows:
2020-08-22 13:51:57
2020-08-22 14:13:13
2020-08-22 14:16:23
I also tried with a different number format as suggested. I also tried changing the column type to DATETIME2(n):
2020-08-22T13:51:57
2020-08-22T14:13:13
2020-08-22T14:16:23
I have no idea what else to review.
I would appreciate any help.
Thanks!
There are many formats of string literals to be converted to dates & times supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not. And the DATETIME datatype in particular is notoriously picky about what formats of string literals work - and which others (most) don't.... DATETIME2(n) is much more forgiving and less picky to deal with!
The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.
The ISO-8601 format is supported by SQL Server comes in two flavors:
YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!
or:
YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.
Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.
The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
In your case, I'd try one of two things:
if you can - use DATETIME2(n) instead of DATETIME as your column's datatype - that alone might solve all your problems
if you can't use DATETIME2(n) - try to use 2020-08-22T13:51:57 instead of
2020-08-22 13:51:57 for specifying your date&time in the CSV import file.
An application is passing the below query to the SQL server and I'm receiving an exception from SQL server as The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
update Images set Created_DATE='23/09/2020 11:00:09'
where ID = 10
Additionally, I cant see the below error in Profiler.
What I've tried is,
Changes the Date format of SQL server as DMY
Change the language to en-GB
I can't change the code so how to make this work by changing SQL server configuration?
Try this:
update Images
set Created_DATE=CONVERT(DATETIME, N'23/09/2020 11:00:09', 103)
where ID = 10
You need to convert the string to date setting a culture. Otherwise, the engine is not able to understand the format as there are various formats.
I believe, your date type is DATETIME, and that's why you are getting this error:
SELECT CAST('23/09/2020 11:00:09' AS DATETIME);
result as:
Msg 242, Level 16, State 3, Line 4 The conversion of a varchar data
type to a datetime data type resulted in an out-of-range value.
for DATETIME2 it is:
Msg 241, Level 16, State 1, Line 4 Conversion failed when converting
date and/or time from character string.
Have a table I'm logging information from a .NET program into.
The VB.NET app explicity dictates the format of the DATETIME string like below
responsedt = Date.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
I then pass this into an INSERT statement that updates my table, however even though the entire setup of the SQL Server is en-GB (British English) the DateTime has gone in the following format:
2019-09-05 19:09:34.823
This was done yesterday so actually should be
2019-05-09 19:09:34.823
The day and month should be switched around, I have tried performing an update on the table post process to get it to update using the following code
FORMAT (xa.daterequested, 'yyyy-dd-MM HH:MM:ss.fff', 'en-gb')
How while this works in a SELECT statement it doesn't seem to work when I do the UPDATE statement.
It is not ideal to have to update all the records dates after the initial INSERT so a solution to either the .NET side of the issue or the SQL would be appreciated as its pickling my head.
You have 2 options to prevent the error from happening again:
Keep dates as date/time data types instead of converting them to strings.
Use formats that are not language or settings dependent. In SQL Server these would be YYYYMMDD hh:mm:ss.msss OR YYYY-MM-DDThh:mm:ss.mss (notice the T between date and time)
To correct the dates already inserted you could use the format codes in the CONVERT function.
UPDATE t SET
daterequested = STUFF( STUFF( StringDate, 5, 0, SUBSTRING(StringDate,7,2)), 9, 2, '')
FROM YourTable t
CROSS APPLY( SELECT CONVERT( varchar(25), '20190905 19:09:34.823', 121) AS StringDate) AS x;
I am running SQL Server 2008 R2 and have a linked server connection to a Postgres database, and use openquery() to communicate between the two. If I create a view on sql server from the Postgres database all field types are char regardless of the field type on my Postgres database.
For example - this query
SELECT *
FROM OPENQUERY(postgrez,
'Select
badgenumber As "badgenum"
,lastunlockdate As "Last Unlock"
,currentDate As "Today"
From doorswipesystem')
Has all fields as char on my SQL Server side - but on the Postgres side badgenumber is an int, and the two date fields are dates. How can I in my OpenQuery keep the original data type?
EDIT
I tried the suggestion posted in my comments by #Kentaro and used this syntax
SELECT Cast(badgenum As Int) As badgenum, Cast(lastunlockdate as datetime) As lastunlockdate,
Cast(Today As datetime) as currentDate
FROM OPENQUERY(postgrez,
'Select
badgenumber As "badgenum"
,lastunlockdate As "Last Unlock"
,currentDate As "Today"
From doorswipesystem')
but I got a compile error of:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I verified field types in my postgres database as well and they are int & date. The only thing that I can think of that could be causing the issue is NULL dates for the two date fields?
I have a SQL Server and Cache server and need to combine some data. Most all functions are working, except when I attempt to pass a date with a variable or a parameter.
Below is a test I ran (one of many).
declare #myDate datetime
set #myDate = convert(datetime,'2012-02-01',120)
select * from ccms..dbo.dcdnstat where timestamp > '2012-02-01' -- WORKS
exec( 'select * from dbo.dcdnstat where cdn = ?', 21004) at ccms -- WORKS
exec( 'select * from dbo.dcdnstat where timestamp > ?',#myDate) at ccms -- ERROR 7213
select * from ccms..dbo.dcdnstat where timestamp > #myDate -- ERROR 7322
Msg 7213, Level 16, State 1, Line 9 The attempt by the provider to
pass remote stored procedure parameters to remote server 'ccms'
failed. Verify that the number of parameters, the order, and the
values passed are correct. Msg 7322, Level 16, State 2, Line 11 A
failure occurred while giving parameter information to OLE DB provider
"MSDASQL" for linked server "ccms".
I have tried different date formats, and as shown above I can query on other fields with variables and I can query on date if I use a specific value.
Instead of a "DATETIME" type for your stored procedure parameter, just use "DATE"
(as long as you don't need the time requirements)
I had a problem similar to this today. It had to do with the fact that the calling server and the linked server were not the same version. One was SQL Server 2005 and the other was SQL Server 2008. The problem was due to the fact that the column being queried against at the remote end (2008) was a datetime2 and the calling server (2005) does not support datetime2.
I know this is a little late but I stumbled upon it looking for something similar with the Cache system. Looks like we are working on the same backend system. Mine is the Avaya CCMS system. Here is what I do to pass dates as variables:
DECLARE #myDate DATETIME
SET #myDate = CAST('2012-07-01' AS DATETIME)
SELECT SUM(CallsOffered), SUM(CallsAnswered), SUM(SkillsetAbandonedAftThreshold)
FROM AvayaCCMS..dbo.mSkillsetStat
WHERE Timestamp = #myDate
I was doing something very similar using a Sybase database as the linked server. Just as MobileMon said, I was able to change the datetime to date and it works fine now.
declare #myDate date
set #myDate = convert(datetime,'2012-02-01',120)