물론 이것은 매우 나쁜 데이터베이스 디자인입니다. 날짜를 저장하려면 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' 칼럼을 제외하고'월요일 '행을 그룹화하는 것이 있습니까? –