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
 VARCHAR + '-' +

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-24 : 05:05:21
HI,

I have an error in this query.

Any idea why?

USE my_database
ALTER TABLE my_table
ADD New_column AS ((MONTH(REPARATION_DATE) AS varchar(2)) + '-' + YEAR(REPARATION_DATE) AS varchar(4)))

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-24 : 05:08:54
you need to use CONVERT() or CAST() to convert it to varchar


ADD New_column AS (CAST (MONTH(REPARATION_DATE) AS varchar(2)) + '-' + CAST (YEAR(REPARATION_DATE) AS varchar(4)))



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-24 : 05:14:08
Perfect!!!!

Thanks so much






quote:
Originally posted by khtan

you need to use CONVERT() or CAST() to convert it to varchar


ADD New_column AS (CAST (MONTH(REPARATION_DATE) AS varchar(2)) + '-' + CAST (YEAR(REPARATION_DATE) AS varchar(4)))



KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-24 : 15:42:39
why should you add this column to table itself? I would have stored only base column (REPARATION_DATE) in table and would have done this only at front end to display the data in required format using formatting functions.

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

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-30 : 05:08:47
or use a computed column as part of SELECT statement to have that

Madhivanan

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

- Advertisement -