Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 [SQL Server]Conversion failed when converting date

Author  Topic 

cms9651
Starting Member

28 Posts

Posted - 2013-02-15 : 07:48:38
hi there, hope in your help.

I need convert the nvarchar(255) field [myDates] to Datetime.
I tried this query, but I've the error.

I'm not sure where to go from here. Any help would be greatly appreciated.

SELECT
CONVERT (
DATETIME,
[DB1].[tkt].[myDates],
103
)
FROM
[DB1].[tkt];


[Err] 22007 - [SQL Server]Conversion failed when converting
date and/or time from character string.


DROP TABLE [DB1].[tkt]
GO
CREATE TABLE [DB1].[tkt] (
[myDates] nvarchar(255) NULL
)


GO
DBCC CHECKIDENT(N'[DB1].[tkt]', RESEED, 3806913)
GOINSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:34');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:28');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:38');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:28');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:29');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:27');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 11:19');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:27
15/02/2013 10:27');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:33');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:28');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:32');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:27');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:45');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:27');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:39');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:26');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:31');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:26');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:39');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:26');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:44');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:26');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:26');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 11:49');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:31
15/02/2013 10:31');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:24');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:30');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:25');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:25');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:24');
GO

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-15 : 07:54:15
Your sample data shows data such as the one shown in red. Did you really insert those into the table? If you did, that is the reason:
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 11:19');
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:27
15/02/2013 10:27'
);
GO
INSERT INTO [tkt] ([myDates]) VALUES ('15/02/2013 10:33');
GO
In any case, it might be a good idea to make the data type of the column DATETIME, and make sure the values that you insert into the column can be converted to DATETIME before you insert the data.
Go to Top of Page

cms9651
Starting Member

28 Posts

Posted - 2013-02-15 : 08:01:56
thank you for reply.
I'm not an administrator of the database and I can't change the table.
Can't be excluded in the query the data shown in red?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-15 : 08:13:07
see

http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-02-15 : 08:48:23
quote:
Originally posted by cms9651

thank you for reply.
I'm not an administrator of the database and I can't change the table.
Can't be excluded in the query the data shown in red?



If you have only DateTime data as shown above, then try this
-- Update your Table as follows
UPDATE [DB1].[tkt] SET [myDates] = LEFT([myDates], 16)
GO
SELECT
CONVERT (
DATETIME,
[DB1].[tkt].[myDates],
103
)
FROM
[tkt];
GO

NOTE:
Whenever you want to store DATETIME Values, must use DATETIME Datatype


--
Chandu
Go to Top of Page

cms9651
Starting Member

28 Posts

Posted - 2013-02-15 : 09:01:14
thanks a lot!
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-02-15 : 09:12:17
quote:
Originally posted by cms9651

thanks a lot!

Welcome

--
Chandu
Go to Top of Page
   

- Advertisement -