当然,这是非常糟糕的数据库设计。要存储日期,您需要使用DATE and TIME类型。
试试这个:
declare @tbl table(id int, dayoftheweek varchar(50), timeoftheday varchar(10))
insert into @tbl values (1, 'mondayOpen', '0:00')
insert into @tbl values (1, 'mondayClose', ' 23:59')
insert into @tbl values (1, 'tuesdayOpen', ' 0:00')
insert into @tbl values (1, 'tuesdayClose', ' 23:59')
insert into @tbl values (1, 'wednesdayOpen', '0:00')
insert into @tbl values (1, 'wednesdayClose', '23:59')
insert into @tbl values (1, 'thursdayOpen', ' 0:00')
insert into @tbl values (1, 'thursdayClose', '23:59')
insert into @tbl values (1, 'fridayOpen', '0:00')
insert into @tbl values (1, 'fridayClose', '23:59')
insert into @tbl values (1, 'saturdayOpen', ' 0:00')
insert into @tbl values (1, 'saturdayClose', '23:59')
insert into @tbl values (1, 'sundayOpen', '0:00')
insert into @tbl values (1, 'sundayClose', '23:59')
insert into @tbl values (2, 'mondayOpen', '0:00')
insert into @tbl values (2, 'mondayClose', ' 23:59')
insert into @tbl values (2, 'tuesdayOpen', ' 0:00')
insert into @tbl values (2, 'tuesdayClose', ' 23:59')
insert into @tbl values (2, 'wednesdayOpen', '0:00')
insert into @tbl values (2, 'wednesdayClose', '23:59')
insert into @tbl values (2, 'thursdayOpen', ' 0:00')
SELECT tOpen.id, tOpen.DayOfTheWeek, tOpen.timeoftheday OpenTime, tClose.timeoftheday CloseTime
FROM (SELECT REPLACE(dayoftheweek,'open','') DayOfTheWeek, timeoftheday,id
FROM @tbl
WHERE LEN(REPLACE(dayoftheweek,'open',''))<LEN(dayoftheweek)) tOpen
JOIN
(SELECT REPLACE(dayoftheweek,'close','') DayOfTheWeek, timeoftheday,id
FROM @tbl
WHERE LEN(REPLACE(dayoftheweek,'close',''))<LEN(dayoftheweek)) tClose ON tOpen.DayOfTheWeek=tClose.DayOfTheWeek AND tOpen.id=tClose.id
每行有'id = 1'?除了包含“星期一”的“dayoftheweek”列之外,是否有任何将“星期一”行组合在一起的内容? –