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 2005 Forums
 SQL Server Administration (2005)
 Concatenate

Author  Topic 

sunny_10
Yak Posting Veteran

72 Posts

Posted - 2014-10-29 : 10:56:18
Hi

I have 1 Table with fields Month & Year . In Month field month is stored & in Year field year is stored. I have created another field Date , Data Type is Date . I want it should be replaced by 01 / month value / year value . What should be the query.

If value in month is 7 & value in year is 2014 then i want 01/07/2014 in Date field
If value in month is 6 & value in year is 2013 then i want 01/06/2013 in Date field

Table name is Test.

Thanks

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-29 : 12:23:17
First off, Date is not a valid datatype in SQL Server 2005 though it has datetime and smalldatetime. Assuming you want datetime, I did it like this:


drop table test
create table test ([month] int, [year] int, [date] datetime);
insert into test([month], [year])
select 7, 2014
union all
select 6, 2013;

update test
set [date] = dateadd(month, [month]-1, dateadd(year, [year]-1900,0))
from test;

select * from test
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-11-05 : 06:26:18
Another method is

update test
set [date] = cast(year*10000+month*100+1 as char(8))
from test;


Madhivanan

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

- Advertisement -