There are over 100 rows, I need to update the entire table so that each MIN is incremented by 10 and once that incrementation reaches 50, the HOUR row is incremented by 1.
There is of course and ID column and I have a where portion of the update clause that will be the same. I tried it by this, but this didn't work, I can't seem to break out once it hits 59.
declare @min int
declare @hour int
set @min=0
set @hour=6
if @min = 59
set @hour = @hour + 1
update dbo.Jobs set @min = starttimeminute = @min + 1
update dbo.Jobs set starttimehour = @hour, @min = 0
I'm a bit confused, since you said "increment by 10" but then added "1".
But something like this:
UPDATE dbo.Jobs
SET min = CASE WHEN min >= 50 THEN (min + 10) - 60 ELSE min + 10 END,
hour = CASE WHEN min >= 50
THEN CASE WHEN hour = 23 THEN 0 ELSE hour + 1 END
ELSE hour END
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[ScheduleJobs](
[ID] [int] IDENTITY(1,1) NOT NULL,
[starttimehour] [int] NULL,
[starttimeminute] [int] NULL,
[startTimeAmPm] [varchar](50) NULL,
CONSTRAINT [PK_ScheduleJobs] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
The data is pretty simple. Fill the starttimehour with 7, fill the startimemintue with 0 and fill the AM/PM portion with AM.
declare @add_min int=10;
update s
set s.starttimehour=s.starttimehour+floor((s.starttimeminute+t.mins)/60)
,s.starttimeminute=(s.starttimeminute+t.mins)%60
,s.starttimeampm=convert(varchar(7)
,dateadd(hour
,s.starttimehour+floor((s.starttimeminute+t.mins)/60)
,dateadd(minute
,(s.starttimeminute+t.mins)%60
,cast('00:00' as time)
)
)
,0
)
from dbo.schedulejobs as s
inner join (select ss.id
,(row_number() over(order by ss.id)-1)*@add_min as mins
from dbo.schedulejobs as ss
) as t
on t.id=s.id
;
Or if you want to be able to subtract aswell:
declare @add_min int=-7;
update s
set s.starttimehour=datepart(hour
,dateadd(hour
,s.starttimehour+floor((s.starttimeminute+t.mins)/60)
,dateadd(minute
,(s.starttimeminute+t.mins)%60
,cast('00:00' as time)
)
)
)
,s.starttimeminute=datepart(minute
,dateadd(hour
,s.starttimehour+floor((s.starttimeminute+t.mins)/60)
,dateadd(minute
,(s.starttimeminute+t.mins)%60
,cast('00:00' as time)
)
)
)
,s.starttimeampm=convert(varchar(7)
,dateadd(hour
,s.starttimehour+floor((s.starttimeminute+t.mins)/60)
,dateadd(minute
,(s.starttimeminute+t.mins)%60
,cast('00:00' as time)
)
)
,0
)
from dbo.schedulejobs as s
inner join (select ss.id
,(row_number() over(order by ss.id)-1)*@add_min as mins
from dbo.schedulejobs as ss
) as t
on t.id=s.id
;
Hi I have got the below .. Please see if it works for you
SET statistics time, io ON;
go
-- dropping temp tables
IF Object_id('tempdb.dbo.#ScheduleJobs', 'U') IS NOT NULL
DROP TABLE #schedulejobs
IF Object_id('tempdb.dbo.#tally1', 'U') IS NOT NULL
DROP TABLE #tally1
go
-- creating tables
CREATE TABLE #schedulejobs
(
id INT IDENTITY (1, 1),
[starttimehour] [INT] NULL,
[starttimeminute] [INT] NULL,
CONSTRAINT [PK_ScheduleJobs] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (
pad_index = OFF, statistics_norecompute = OFF, ignore_dup_key = OFF,
allow_row_locks = on, allow_page_locks = on) ON [PRIMARY]
)
ON [PRIMARY]
CREATE TABLE #tally1
(
rownum1 INT NULL,
min1 INT NULL
)
go
-- Populate tables with data
DECLARE @ID INT = 1
WHILE @ID < 1001
BEGIN
INSERT INTO #schedulejobs
(starttimehour,
starttimeminute)
VALUES (7,
0)
SET @ID = @ID + 1
END
go
INSERT INTO #tally1
VALUES (1,
0),
(2,
10),
(3,
30),
(4,
40),
(5,
50),
(6,
59)
go
-- Final Select to give Result
SELECT a.starttimehour,
a.starttimeminute,
( a.starttimehour + a.batchno ) % 24,
b.min1,
CASE
WHEN ( ( a.starttimehour + a.batchno ) % 24 ) BETWEEN 0 AND 12 THEN
'AM'
ELSE 'PM'
END AS AMPM
FROM (SELECT id,
1 + ( ( Row_number()
OVER (
ORDER BY id) - 1 ) % 7 ) AS
rownum,
Floor(Cast(Row_number()
OVER (
ORDER BY (SELECT 1)) AS DECIMAL(10, 2)) / 7) AS
BatchNo,
Floor(Cast(Row_number()
OVER (
ORDER BY (SELECT 1)) AS DECIMAL(10, 2)) / 7) AS
BatchNo1,
starttimehour,
starttimeminute
FROM #schedulejobs) a
JOIN #tally1 b
ON a.rownum = b.rownum1
go
SET statistics time, io OFF;