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.
Author |
Topic |
Junior Sqler
Starting Member
18 Posts |
Posted - 2013-12-19 : 05:32:08
|
Hello!I have created a table Abc and i want to update its rows only every first day of year. The following is not working.Could you help me?CREATE TABLE Abc ( A int,B int,C int)insert into Abccase when month(getdate())=1 and day(getdate()=1 then(select a1, b1, c1from dbo.table awhere a1= '###')end |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-12-19 : 06:11:57
|
CREATE TABLE Abc ( A int,B int,C int)insert into Abcselect a1, b1, c1from dbo.table awhere a1= '###' AND month(getdate())=1 and day(getdate()=1 Do you want to manually run the above script or want to set automatic run by SQL Server Agent Jobs?--Chandu |
|
|
Junior Sqler
Starting Member
18 Posts |
Posted - 2013-12-19 : 06:25:55
|
This script is part of code that will run automatically daily by sql server agent jobs.. but this specific script will need to run only once per yearThe problem is that variable C=avg(cost) of previous year for a specific category(b1).. For example if avg cost for yr 2012 is 100 in table ABC i would like to have A=2013 B(name of category) and C=100..For that reason i do not think that your proposal will work for me.. Actually the real code is like this:CREATE TABLE Abc ( A date, B varchar(20),C int)insert into Abccase when month(getdate())=1 and day(getdate()=1 then(select a1,b1 avg(c1)from dbo.table awhere a1= YEAR(GetDate())-1 groub by a1,b1)end |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-12-19 : 06:32:01
|
Scipt part so you can make use of IF...ELSE statementif (month(getdate())=1 and day(getdate()=1 )BEGINinsert into Abc(select a1,b1 avg(c1)from dbo.table awhere a1= YEAR(GetDate())-1 groub by a1,b1)end--Chandu |
|
|
Junior Sqler
Starting Member
18 Posts |
Posted - 2013-12-19 : 06:45:13
|
oook!!thank you very much!! |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-12-19 : 06:48:31
|
welcome --Chandu |
|
|
ScottPletcher
Aged Yak Warrior
550 Posts |
Posted - 2013-12-19 : 18:33:20
|
if datepart(dayofyear, getdate()) = 1 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2013-12-24 : 08:02:33
|
ScottPletcher's code is simpler. That's definitely a celver use of datepart function MadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|