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
 General SQL Server Forums
 New to SQL Server Programming
 arithmetic overflow error converting to datetime
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learning_grsql
Posting Yak Master

155 Posts

Posted - 08/18/2012 :  11:13:49  Show Profile  Reply with Quote
Hi,

I have a date column with varchar(50) as datatype with values like
01/08/2012
02/08/2012
03/08/2012

When I try to convert this in '20120801' format using the code below, i get the error arithmetic overflow error converting expression to data type datetime

select convert(varchar(8),cast(date as datetime),112) as date, field1 from mytable

Edited by - learning_grsql on 08/18/2012 11:14:36

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 08/18/2012 :  11:19:17  Show Profile  Reply with Quote
no need of inner cast

select convert(varchar(8),[date],112) as date, field1 from mytable


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


Edited by - visakh16 on 08/18/2012 11:19:30
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 08/18/2012 :  11:22:06  Show Profile  Reply with Quote
b/w one question is the original values in dd/mm/yyyy or mm/dd/yyyy format?

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

Go to Top of Page

learning_grsql
Posting Yak Master

155 Posts

Posted - 08/18/2012 :  11:22:55  Show Profile  Reply with Quote
select convert(varchar(8),[date],112) as date, field1 from mytable

The above code displays date as
'01/08/20'
'02/08/20'
'03/08/20'

But I want output as
'20120801'
'20120802'
'20120803'

@visakh
I just read your second post..
original values are 'dd/mm/yyyy'

Edited by - learning_grsql on 08/18/2012 11:26:21
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 08/18/2012 :  11:28:07  Show Profile  Reply with Quote

select convert(varchar(8),convert(datetime,[date],103),112) as [date], field1 from mytable


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


Edited by - visakh16 on 08/18/2012 11:28:36
Go to Top of Page

learning_grsql
Posting Yak Master

155 Posts

Posted - 08/18/2012 :  13:21:11  Show Profile  Reply with Quote
As usual, many thanks visakh16. It works now.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 08/18/2012 :  15:16:47  Show Profile  Reply with Quote
welcome

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

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22461 Posts

Posted - 09/03/2012 :  10:09:46  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
1 Always use proper DATETIME datatype to store dates
2 Formation matters only when you want to show them somewhere

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 09/03/2012 :  15:12:31  Show Profile  Reply with Quote
i suggested this already but this was the reason OP told for that

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=178038

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

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