如何让这个 SQL 语句返回空字符串而不是 NULL?

发布于 2024-07-13 17:49:19 字数 1032 浏览 5 评论 0原文

LastAccessed=(select max(modifydate) from scormtrackings WHERE 
bundleid=@bundleid and userid=u.userid),
CompletedLessons=(select Value from scormtrackings WHERE 
bundleid=@bundleid and userid=u.userid AND param='vegas2.progress'),
TotalLessons=100,
TotalNumAvail=100,
TotalNumCorrect=(SELECT Value FROM scormtrackings WHERE 
bundleid=@bundleid AND userid=u.userid AND param='cmi.score.raw')

这只是我的 ASP.NET Repeater 使用的大型选择语句的一部分,当值为 NULL 时,该语句不断崩溃,我尝试过 ISNULL() 但要么它不起作用,要么我做错了。

ISNULL((SELECT max(modifydate) FROM scormtrackings WHERE 
bundleid=@bundleid AND userid=u.userid),'') AS LastAccessed,

(...)

???

更新:我已经尝试了所有这些方法,返回 '', 0, 1,而不是返回 null 的值,但它仍然不起作用,我想知道问题是否出在 Repeater

相关问题:

为什么我的中继器在 Eval(NULL) 上不断崩溃价值观?

LastAccessed=(select max(modifydate) from scormtrackings WHERE 
bundleid=@bundleid and userid=u.userid),
CompletedLessons=(select Value from scormtrackings WHERE 
bundleid=@bundleid and userid=u.userid AND param='vegas2.progress'),
TotalLessons=100,
TotalNumAvail=100,
TotalNumCorrect=(SELECT Value FROM scormtrackings WHERE 
bundleid=@bundleid AND userid=u.userid AND param='cmi.score.raw')

This is only part of a large select statement used by my ASP.NET Repeater that keeps crashing when the values are NULL, I have tried ISNULL() but either it didn't work, or I did it wrong.

ISNULL((SELECT max(modifydate) FROM scormtrackings WHERE 
bundleid=@bundleid AND userid=u.userid),'') AS LastAccessed,

(...)

???

UPDATE: I've tried all these things with returning '', 0, 1, instead of the value that would be null and it still doesn't work, I wonder if the problem is with the Repeater?

Related Question:

Why does my repeater keep crashing on Eval(NULL) values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

琉璃繁缕 2024-07-20 17:49:19

您可以使用 COALESCE 函数来避免获取空值。 基本上它返回列表中的第一个非空值。

SELECT COALESCE(dateField, '') FROM Some_Table

这是一个 ANSI 标准,但我注意到它在 Access SQL 中不可用。

You can use the COALESCE function to avoid getting nulls. Basically it returns the first non-null value from the list.

SELECT COALESCE(dateField, '') FROM Some_Table

This is an ANSI standard, though I have noticed that it isn't available in Access SQL.

巴黎夜雨 2024-07-20 17:49:19

您可以使用案例:

CASE WHEN MyField IS Null THEN ''
ELSE MyField
End As MyField

You can use CASE:

CASE WHEN MyField IS Null THEN ''
ELSE MyField
End As MyField
夜深人未静 2024-07-20 17:49:19
select max(isnull(modifydate, "default date") from scormtrackings where...

只要至少有一行满足 where 子句就可以工作,否则您仍然会得到 NULL

select IsNull( max(modifydate), "default_date") from scormtrackings where ...

应该在所有情况下都可以工作

select max(isnull(modifydate, "default date") from scormtrackings where...

will work as long as there is at least one row that satisfies the where clause, otherwise you will still get NULL

select IsNull( max(modifydate), "default_date") from scormtrackings where ...

should work in all cases

去了角落 2024-07-20 17:49:19

您可以使用NVL

NVL(<可能的 null 值>,)

You can use NVL.

NVL(<possible null value>,<value to return when arg1 is null>)

少女七分熟 2024-07-20 17:49:19
select max(isnull(Date,"default date")) .....
select max(isnull(Date,"default date")) .....
做个ˇ局外人 2024-07-20 17:49:19

这也很有效...不愿承认我使用它的频率!!

声明 @LastAccessed varchar(30)

从 scormtrackings 选择 @LastAccessed = max(modifydate)

设置 @LastAccessed = isnull(@LastAccessed,'')

选择 @LastAccessed 作为 LastAccessed

This works too... hate to admit how often I use it!!

Declare @LastAccessed varchar(30)

Select @LastAccessed = max(modifydate) from scormtrackings

Set @LastAccessed = isnull(@LastAccessed,'')

Select @LastAccessed as LastAccessed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文