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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Save Month & Year

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2012-08-16 : 16:39:13
Hi There,

I need to update a table field with the Month & Year of a field with a date in it.

I have tried this:
UPDATE Rental
SET MonYear = MONTH(RentalStartDate) + YEAR(RentalStartDate)

But where the month is 08 and the year is 2012 I get a result of 2020 (08 + 2012 = 2020) instead of 082012 as a concate of the two

Any help would be appreciated.

Best regards,




Steve

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-16 : 16:49:13
[code]
UPDATE Rental
SET MonYear = CAST(MONTH(RentalStartDate) AS varchar(2)) + CAST( YEAR(RentalStartDate) AS varchar(4))
[/code]


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

Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2012-08-17 : 04:18:16
Hi Visakh,

Thaks for your reply.

That's perfect, thank you.

Best Regards,



Steve
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-17 : 10:22:56
welcome

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

Go to Top of Page
   

- Advertisement -