开发者

Concerned with replicate fn in sql

I am using following query

create table  #Attendence (id int identity(1,1),det varchar(2000))
insert into #Attendence (det )
select  --convert(char(10),@Date,3) +REPLICATE(' ', 20 - LEN(convert(char(10),@Date,3)))+
        staff.StaffNAme +REPLICATE(' ', 20 - LEN(staff.StaffNAme ))+
        case Att.FN when 1 then 'Present'
                else 'Absent'
        end  +REPLICATE(' ', 20 - LEN(case Att.FN when 1 开发者_开发问答then 'Present'
                else 'Absent'
        end ))+
        case Att.AN when 1 then 'Present'
                else 'Absent'
        end +REPLICATE(' ', 20 - LEN(case Att.AN when 1 then 'Present'
                else 'Absent'
        end  ))
from  Staff_Details staff
      inner join
        STAFF_Attendance att
            on staff.staffid=att.staffId
Select * from   #Attendence

The problem is in the resultset as the length of STaffName increases, the values in the right of staffname is moving rightward.Can anybody help to make each column fixed length?


create table  #Attendence (id int identity(1,1),det varchar(2000))
insert into #Attendence (det )
select  --cast(convert(char(10),@Date,3) as char(20)) +
        cast(staff.StaffNAme as char(20)) +
        cast(case Att.FN when 1 then 'Present' else 'Absent' end as char(20)) +
        cast(case Att.AN when 1 then 'Present' else 'Absent' end as char(20))
from  Staff_Details staff
      inner join
        STAFF_Attendance att
            on staff.staffid=att.staffId
Select * from   #Attendence


I guess Staffname has trailing spaces. These will be ignored by LEN.

Try this to force a length of 20 chars exactly:

LEFT(staff.StaffNAme + '                    ', 20)

...instead of...

staff.StaffNAme + REPLICATE(' ', 20 - LEN(staff.StaffNAme))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜