sqlserver判断非null

with a as (
select '1' id
union all 
select '2' id
)
select dbo.f_getId(a.id),a.id,case when len(rtrim(ltrim(a.id))) = 0 then 1 else 2 end as idss from mytb
left join a on a.id = mytb.id
/*
(无列名)    id    idss
1    1    2
1    2    2
0    NULL    2
*/

select dbo.f_getId(null) --0
--测试
alter function f_getId(
    @id varchar(100)
)
returns int
as
begin
    if(ISNULL(@id,'')='')
    begin
     return 0
    end

    return 1
end

--建表
create table mytb(
    id varchar(100),
    name varchar(100)
)

insert into mytb(id,name)
values('1','2')
insert into mytb(id,name)
values('2','3')
insert into mytb(id,name)
values('3','4')

 

posted @ 2022-01-20 23:36  vba是最好的语言  阅读(243)  评论(0)    收藏  举报