SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 [SQL Server]Conversion failed when converting date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cms9651
Starting Member

28 Posts

Posted - 02/15/2013 :  07:48:38  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/15/2013 :  07:54:15  Show Profile  Reply with Quote
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 - 02/15/2013 :  08:01:56  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 02/15/2013 :  08:13:07  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 02/15/2013 :  08:48:23  Show Profile  Reply with Quote
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

Edited by - bandi on 02/15/2013 08:49:33
Go to Top of Page

cms9651
Starting Member

28 Posts

Posted - 02/15/2013 :  09:01:14  Show Profile  Reply with Quote
thanks a lot!
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 02/15/2013 :  09:12:17  Show Profile  Reply with Quote
quote:
Originally posted by cms9651

thanks a lot!

Welcome

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000