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
 General SQL Server Forums
 New to SQL Server Programming
 Subtract

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2013-05-02 : 05:05:23
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

52326 Posts

Posted - 2013-05-02 : 05:29:54
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-02 : 05:33:38
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 - 2013-05-02 : 06:28:09
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-02 : 06:33:22
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

52326 Posts

Posted - 2013-05-02 : 07:04:56
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
   

- Advertisement -