LoginSignup
0
0

More than 5 years have passed since last update.

カウントアップして日またぎでリセットするSQL

Posted at

1.テーブルを作ります

create
create table indexT
(
    indexNo int not NULL,
    UpdateDatetime datetime,
    Primary Key( indexNo )
);

2.SQLを実行します。

count_up
update [indexT] set 
[indexNo] = ( case when CAST([UpdateDateTime] as DATE) = CAST(getdate() as DATE) then [indexNo] + 1 else 1 end ), 
[UpdateDateTime]= getdate();

insert into [indexT] ([indexNo], [UpdateDateTime]) 
select 1 as indexNo, getdate() as UpdateDateTime 
where NOT EXISTS (select 1 from indexT);

select [indexNo], [UpdateDateTime] from [indexT];
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0