可在 SQL Server 和 MS Access 中使用的 COALESCE、IFNULL 或 NZ() 函数
我有一个可以使用 SQL Server 或 MS Access 作为数据存储的项目。在一个 SELECT 语句中,我必须对单个列和单个值执行 COALESCE 操作,如下所示:
SELECT COALESCE([Amount], 0) FROM PaymentsDue;
我想编写一条可以在 SQL Server 和 MS Access 中正确执行的 SQL 语句。最受关注的 SQL Server 版本是 2008,不过跨版本适用的解决方案将是首选。
今天早些时候,有人能够 向我展示一个 SQL 技巧,它允许我使用单个 SELECT 语句有效地将 DATETIME 转换为 DATE。我想知道是否有人有类似的技巧来以可应用于 SQL Server 和 MS Access 的方式执行 COALESCE(例如 IFNULL 或 NZ)操作?
I have a project that can use either SQL Server or MS Access as the data store. In one SELECT statement, I must perform a COALESCE operation on a single column and a single value, like this:
SELECT COALESCE([Amount], 0) FROM PaymentsDue;
I would like to write a single SQL statement that will execute correctly in both SQL Server and MS Access. The SQL Server version that is of immediate interest is 2008, although a solution applicable across versions would be preferred.
Earlier today, someone was able to show me an SQL trick that allowed me to use a single SELECT statement to effectively CAST a DATETIME to DATE. I was wondering if anyone has a similar trick to perform a COALESCE (eg, IFNULL or NZ) operation in a way that can be applied to both SQL Server and MS Access?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为没有任何语法在两个平台上具有相同的功能。
注意
Nz()
仅在使用 Access 用户界面时可用。以下是一些可以相当容易地转换为 COALESCE 的建议,尽管重复该专栏很痛苦:
示例 1:
示例 2:
I don't think there is any syntax that functions the same on both platforms.
Note
Nz()
is only available when using the Access user interface.Here are a couple of suggestions that can be transformed to
COALESCE
fairly easily, though repeating the column is a pain:Sample 1:
Sample 2:
这会起作用,但很笨重:
显然,如果您有多个列,这很快就会变得难以管理。
This will work, but it's clunky:
Obviously if you have more than one column, this gets to be quickly unmanageable.
在模块中创建自定义公共函数。
添加错误处理等,进行改进。
现在,您可以在 MS Access 和 SQL 中使用
COALESCE
函数。Create a custom public function in a module.
Add in error handling, etc., make improvements.
Now, you would be able to use the
COALESCE
function in MS Access and SQL.我猜你不想编写一个解析器来管理 Jet SQL 和 T-SQL 之间的翻译...
我们开发的一个解决方案(是的,我们有一个类似的问题需要解决)是定义一些“伪元语言”我们在元 SQL 语法中使用它,并且我们有一种从这种元语言到 Jet SQL 或 T-SQL 的转换器。
示例:
相同的策略可用于通配符和分隔符:
我知道它不是那么好,但它非常高效和干净。要点是:
And I guess you do not want to write a parser that will manage translations between Jet SQL and T-SQL ...
A solution that we developped (yes, we had a similar problem to solve) is to define some 'pseudo-metalanguage' that we use in our meta-SQL syntax, and we have a kind of translator from this meta-language into Jet SQL or T-SQL.
Example:
The same strategy could be used for wildcards and delimiters:
I konw it's not that nice, but it is quite efficient and clean. The main points are: