如何用 SQL 查询中的字符串值替换单个数字零“0”(仅一个数字)
How to replace single digit zero '0' (one digit only) with a string value in SQL Query
我想用字符串值 N/A 替换零 '0'。我已经使用了替换函数,但替换函数的问题是它在数字值 (例如 20 到 2N/A) 中每次出现时都会用字符串替换 0。我想用 单个数字 值“0”替换字符串。
我正在使用带有替换功能的当前查询,如下所示:
select left(pdc.semester,6) as Sem1,RIGHT(pdc.semester,4) as sem2,pdc.semester as Semester,replace(count(pdc.CNIC),'0','N/A') as TotalRegistrations,replace(count(s.studentid),'0','N/A') as TotalRegistered,
Replace(count(case when r.Grade is not null then 'Appeared' end),0,'N/A') as Appeared,
Replace(count(case when r.Grade='f' then 'fail' end),'0','N/A') as Failed,
Replace(count(case when r.grade <>'f' then 'pass' end),'0','N/A') as Passed
from PDC_PreRegistration pdc
left join Students s on pdc.Semester=s.CurSemester and s.nic=pdc.CNIC
left join studentresultnet r on s.studentid=r.studentid and s.CurSemester=r.Semester
left join semester se on se.Semester=r.Semester
where pdc.semester
in (select CurSemester from students where batch='PD2110')
group by pdc.Semester
ORDER BY sem2,Sem1
上述查询结果如下:
Sem1
sem2
Semester
TotalRegistrations
TotalRegistered
Appeared
Failed
Passed
NSPP01
2110
NSPP01 2110
2N/A
2N/A
19
2
17
NSPP02
2110
NSPP02 2110
37
35
24
1
23
NSPP01
2111
NSPP01 2111
76
42
N/A
N/A
N/A
NSPP02
2111
NSPP02 2111
121
81
N/A
N/A
N/A
我想用 N/A 替换 '0' 但不是 20 到 2N/A.
SQL 没有替换函数的查询产生以下结果。
select left(pdc.semester,6) as Sem1,RIGHT(pdc.semester,4) as sem2,pdc.semester as Semester,count(pdc.CNIC) as TotalRegistrations,count(s.studentid) as TotalRegistered,
count(case when r.Grade is not null then 'Appeared' end) as Appeared,
count(case when r.Grade='f' then 'fail' end) as Failed,
count(case when r.grade <>'f' then 'pass' end) as Passed
from PDC_PreRegistration pdc
left join Students s on pdc.Semester=s.CurSemester and s.nic=pdc.CNIC
left join studentresultnet r on s.studentid=r.studentid and s.CurSemester=r.Semester
left join semester se on se.Semester=r.Semester
where pdc.semester
in (select CurSemester from students where batch='PD2110')
group by pdc.Semester
ORDER BY sem2,Sem1
以上查询的结果
Sem1
sem2
Semester
TotalRegistrations
TotalRegistered
Appeared
Failed
Passed
NSPP01
2110
NSPP01 2110
20
20
19
2
17
NSPP02
2110
NSPP02 2110
37
35
24
1
23
NSPP01
2111
NSPP01 2111
76
42
0
0
0
NSPP02
2111
NSPP02 2111
121
81
0
0
0
用 N/A 替换“个位数零”的代码是通过一起使用 IsNULL(NULLIF()) 函数。
IsNULL(NULLIF(CAST(count(s.studentid) AS char(20)),'0'), 'N/A')
select left(pdc.semester,6) as Sem1,RIGHT(pdc.semester,4) as sem2,pdc.semester as Semester,IsNULL(NULLIF(CAST(count(s.studentid) AS char(20)),'0'), 'N/A') as TotalRegistrations,count(s.studentid) as TotalRegistered,
Replace(count(case when r.Grade is not null then 'Appeared' end),0,'N/A') as Appeared,
Replace(count(case when r.Grade='f' then 'fail' end),'0','N/A') as Failed,
Replace(count(case when r.grade <>'f' then 'pass' end),'0','N/A') as Passed
from PDC_PreRegistration pdc
left join Students s on pdc.Semester=s.CurSemester and s.nic=pdc.CNIC
left join studentresultnet r on s.studentid=r.studentid and s.CurSemester=r.Semester
left join semester se on se.Semester=r.Semester
where pdc.semester
in (select CurSemester from students where batch='PD2110')
group by pdc.Semester
ORDER BY sem2,Sem1
解释:
select Count(studentid) --结果studentid中的记录数coulmn name.which可以是单个数字“0”或“00”
select CAST(0 as char(20)) --将Count Function的结果int转换为char,然后在Next中将结果与char“0”进行匹配步骤.
SELECT CAST(count(s.studentid) AS char(20))
- SELECT NULLIF(CAST(count(s.studentid) AS char(20)),'0') --NULLIF(expr1, expr2).这里expr1会来自 SQL COUNT 函数作为 char,expr2 是硬编码的 char“0”。
NULLIF的更多解释如下:
select NULLIF('abc','abc') --**Results Null because both expressions inside NullIF have same value.٭٭
select NULLIF('0','0') --**Result Null because both Expressions inside NullIF have same value.٭٭
select NUllIF('20','0') --** Results 20 becuase Expressions inside NullIF have not same value.٭٭
- ---ISNULL (expression, replacement)--- 如果表达式为 NULL 那么 ISNULL 函数将替换为 N/A 在我们的例子中,或者如果该值不为空,则它将保留该值。
select ISNULL(NULL,' Value is NULL so replace by This expression') --**Results in Replace Expression.٭٭
select ISNULL('Any value', 'if the value is null then replace by This expression') --**Results in Any value.٭٭
我想用字符串值 N/A 替换零 '0'。我已经使用了替换函数,但替换函数的问题是它在数字值 (例如 20 到 2N/A) 中每次出现时都会用字符串替换 0。我想用 单个数字 值“0”替换字符串。
我正在使用带有替换功能的当前查询,如下所示:
select left(pdc.semester,6) as Sem1,RIGHT(pdc.semester,4) as sem2,pdc.semester as Semester,replace(count(pdc.CNIC),'0','N/A') as TotalRegistrations,replace(count(s.studentid),'0','N/A') as TotalRegistered,
Replace(count(case when r.Grade is not null then 'Appeared' end),0,'N/A') as Appeared,
Replace(count(case when r.Grade='f' then 'fail' end),'0','N/A') as Failed,
Replace(count(case when r.grade <>'f' then 'pass' end),'0','N/A') as Passed
from PDC_PreRegistration pdc
left join Students s on pdc.Semester=s.CurSemester and s.nic=pdc.CNIC
left join studentresultnet r on s.studentid=r.studentid and s.CurSemester=r.Semester
left join semester se on se.Semester=r.Semester
where pdc.semester
in (select CurSemester from students where batch='PD2110')
group by pdc.Semester
ORDER BY sem2,Sem1
上述查询结果如下:
Sem1 | sem2 | Semester | TotalRegistrations | TotalRegistered | Appeared | Failed | Passed |
---|---|---|---|---|---|---|---|
NSPP01 | 2110 | NSPP01 2110 | 2N/A | 2N/A | 19 | 2 | 17 |
NSPP02 | 2110 | NSPP02 2110 | 37 | 35 | 24 | 1 | 23 |
NSPP01 | 2111 | NSPP01 2111 | 76 | 42 | N/A | N/A | N/A |
NSPP02 | 2111 | NSPP02 2111 | 121 | 81 | N/A | N/A | N/A |
我想用 N/A 替换 '0' 但不是 20 到 2N/A.
SQL 没有替换函数的查询产生以下结果。
select left(pdc.semester,6) as Sem1,RIGHT(pdc.semester,4) as sem2,pdc.semester as Semester,count(pdc.CNIC) as TotalRegistrations,count(s.studentid) as TotalRegistered,
count(case when r.Grade is not null then 'Appeared' end) as Appeared,
count(case when r.Grade='f' then 'fail' end) as Failed,
count(case when r.grade <>'f' then 'pass' end) as Passed
from PDC_PreRegistration pdc
left join Students s on pdc.Semester=s.CurSemester and s.nic=pdc.CNIC
left join studentresultnet r on s.studentid=r.studentid and s.CurSemester=r.Semester
left join semester se on se.Semester=r.Semester
where pdc.semester
in (select CurSemester from students where batch='PD2110')
group by pdc.Semester
ORDER BY sem2,Sem1
以上查询的结果
Sem1 | sem2 | Semester | TotalRegistrations | TotalRegistered | Appeared | Failed | Passed |
---|---|---|---|---|---|---|---|
NSPP01 | 2110 | NSPP01 2110 | 20 | 20 | 19 | 2 | 17 |
NSPP02 | 2110 | NSPP02 2110 | 37 | 35 | 24 | 1 | 23 |
NSPP01 | 2111 | NSPP01 2111 | 76 | 42 | 0 | 0 | 0 |
NSPP02 | 2111 | NSPP02 2111 | 121 | 81 | 0 | 0 | 0 |
用 N/A 替换“个位数零”的代码是通过一起使用 IsNULL(NULLIF()) 函数。
IsNULL(NULLIF(CAST(count(s.studentid) AS char(20)),'0'), 'N/A')
select left(pdc.semester,6) as Sem1,RIGHT(pdc.semester,4) as sem2,pdc.semester as Semester,IsNULL(NULLIF(CAST(count(s.studentid) AS char(20)),'0'), 'N/A') as TotalRegistrations,count(s.studentid) as TotalRegistered,
Replace(count(case when r.Grade is not null then 'Appeared' end),0,'N/A') as Appeared,
Replace(count(case when r.Grade='f' then 'fail' end),'0','N/A') as Failed,
Replace(count(case when r.grade <>'f' then 'pass' end),'0','N/A') as Passed
from PDC_PreRegistration pdc
left join Students s on pdc.Semester=s.CurSemester and s.nic=pdc.CNIC
left join studentresultnet r on s.studentid=r.studentid and s.CurSemester=r.Semester
left join semester se on se.Semester=r.Semester
where pdc.semester
in (select CurSemester from students where batch='PD2110')
group by pdc.Semester
ORDER BY sem2,Sem1
解释:
select Count(studentid) --结果studentid中的记录数coulmn name.which可以是单个数字“0”或“00”
select CAST(0 as char(20)) --将Count Function的结果int转换为char,然后在Next中将结果与char“0”进行匹配步骤.
SELECT CAST(count(s.studentid) AS char(20))
- SELECT NULLIF(CAST(count(s.studentid) AS char(20)),'0') --NULLIF(expr1, expr2).这里expr1会来自 SQL COUNT 函数作为 char,expr2 是硬编码的 char“0”。 NULLIF的更多解释如下:
select NULLIF('abc','abc') --**Results Null because both expressions inside NullIF have same value.٭٭
select NULLIF('0','0') --**Result Null because both Expressions inside NullIF have same value.٭٭
select NUllIF('20','0') --** Results 20 becuase Expressions inside NullIF have not same value.٭٭
- ---ISNULL (expression, replacement)--- 如果表达式为 NULL 那么 ISNULL 函数将替换为 N/A 在我们的例子中,或者如果该值不为空,则它将保留该值。
select ISNULL(NULL,' Value is NULL so replace by This expression') --**Results in Replace Expression.٭٭
select ISNULL('Any value', 'if the value is null then replace by This expression') --**Results in Any value.٭٭