.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Sql Server >> Post New QuestionBookmark and Share Subscribe to Forum

Getting exception -Syntax error converting datetime from character string.

Posted By: Arul     Posted Date: September 29, 2011    Points:5   Category :Sql Server
Hi,

Am getting this exception while executing below dynamic query.

select * from MyTable where CONVERT(DATETIME, CONVERT(CHAR(8),JoinDate)) > GETDATE()

JoinDate in MyTable is a numeric field. Which holds values like '20110929'

Also MyTable have some invalid JoinDate like '201109'. So while executing the query am getting this error.

Can any one tell me how to avoid this error with is single query without having any sub queries.

Also I tried below query, still am getting the error.

select * from MyTable where LEN(JoinDate) = 8 AND CONVERT(DATETIME, CONVERT(CHAR(8),JoinDate)) > GETDATE()

Thanks,
Arul


Responses
Author: Narayanan             
Posted Date: September 29, 2011     Points: 20   

Hi,
refer this link...
http://blogs.msdn.com/b/patrickgallucci/archive/2007/07/31/sql-server-function-to-return-date-from-numeric-value.aspx
http://bytes.com/topic/sql-server/answers/79949-converting-integer-datetime

Regards,
Lakshmi Narayanan.S

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
Author: Arul             
Posted Date: September 30, 2011     Points: 20   

HI,

That links not answered my question. I found the solution.

select * from MyTable where CASE WHEN LEN(JoinDate) = 8 THEN CONVERT(DATETIME, CONVERT(CHAR(8),JoinDate)) ELSE NULL END > GETDATE()

Regards,
Arul


Author: Narayanan             
Posted Date: September 30, 2011     Points: 20   

Hi,
It's great.keep it up

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend