Select dbo.GetDaysFromBString(‘0000001’)
Output: Sunday
Create function [dbo].[GetDaysFromBString](@DayBinaryString varchar(7))
returns varchar(250)
as
begin
declare @Results varchar(250)
--declare @DayBinaryString varchar(7);
--set @DayBinaryString='1111111'
--print @DayBinaryString
;with DaysSelected as
(select
case when SUBSTRING(@DayBinaryString,1,1)=1 then 'Monday,' else''end +
case when SUBSTRING(@DayBinaryString,2,1)=1 then 'Tuesday,'else''end +
case when SUBSTRING(@DayBinaryString,3,1)=1 then 'Wednesday,'else''end +
case when SUBSTRING(@DayBinaryString,4,1)=1 then 'Thursday,'else''end +
case when SUBSTRING(@DayBinaryString,5,1)=1 then 'Friday,'else''end +
case when SUBSTRING(@DayBinaryString,6,1)=1 then 'Saturday,'else''end +
case when SUBSTRING(@DayBinaryString,7,1)=1 then 'Sunday,'else''end as DaysName )
select @Results =SUBSTRING(Daysname,1,len(Daysname)-1) from DaysSelected
Return @Results
end