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
 Subtract
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

145 Posts

Posted - 05/02/2013 :  05:05:23  Show Profile  Reply with Quote
Hi there,

I have the following error:

msg 241, level 16, state 1
Conversion failed when converting date and/or time from character string

Select
CAST (S as varchar (1) ) as S
,S
,Sg
,Cd
,Sa
,Gen
,Id
, cast(DATEDIFF(month,Gen,Sg) as varchar (4)) as PP

from table_a


The data type of Gen is varchar (7) and Sg as well.

Im using this same query in another table with the same data type and works. In this case I just need to subtract from Gen the value of Sg, both values are represented as follow: 10-2012, but the date type is not date is varchar.

Any tip?

Thanks

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/02/2013 :  05:29:54  Show Profile  Reply with Quote
Sg should be a date value so not sure how you will fit it in a varchar(7) field. The format has to be unambiguos. Make sure you read this

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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 05/02/2013 :  05:33:38  Show Profile  Reply with Quote
If you don't have permissions to alter table structure...

--Just check the following illustration:
SET DATEFORMAT DMY
GO
DECLARE @Gen varchar (7) = '10-2012', @Sg varchar(7) = '12-2012'
SELECT cast(DATEDIFF(month,CAST ('01-'+@Gen AS DATE),CAST('01-'+@Sg AS DATE)) AS VARCHAR)as PP

NOTE: Better to change data types of both columns to DATE type

--
Chandu
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 05/02/2013 :  06:28:09  Show Profile  Reply with Quote
Thanks a lot guys.

Its working.




quote:
Originally posted by bandi

If you don't have permissions to alter table structure...

--Just check the following illustration:
SET DATEFORMAT DMY
GO
DECLARE @Gen varchar (7) = '10-2012', @Sg varchar(7) = '12-2012'
SELECT cast(DATEDIFF(month,CAST ('01-'+@Gen AS DATE),CAST('01-'+@Sg AS DATE)) AS VARCHAR)as PP

NOTE: Better to change data types of both columns to DATE type

--
Chandu

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 05/02/2013 :  06:33:22  Show Profile  Reply with Quote
quote:
Originally posted by jfm

Thanks a lot guys.
Its working.
quote:
Originally posted by bandi
NOTE: Better to change data types of both columns to DATE type




Try to follow ISO Date Format with date time types and use DATE/DATETIME data types for date values...

Welcome

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/02/2013 :  07:04:56  Show Profile  Reply with Quote
quote:
Originally posted by bandi

quote:
Originally posted by jfm

Thanks a lot guys.
Its working.
quote:
Originally posted by bandi
NOTE: Better to change data types of both columns to DATE type




Try to follow ISO Date Format with date time types and use DATE/DATETIME data types for date values...

Welcome

--
Chandu


which is what i've explained in link above

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.09 seconds. Powered By: Snitz Forums 2000