如何将Sql转换为Oracle查询

时间:2018-04-08 23:26:06

标签: sql oracle

我有sql查询,如: -

{{1}}

请告诉我如何转换为oracle查询? 提前致谢。 俊

4 个答案:

答案 0 :(得分:3)

在Oracle中,空字符串与NULL相同,因此LENGTH( NULL )LENGTH( '' )都会NULL而非0

所以你的查询是:

SELECT CASE
       WHEN NOT EXISTS(
         SELECT 1 
         FROM   Customers
         WHERE  ContactName IS NULL
       )
       THEN 1
       ELSE 0
       END AS value
FROM   DUAL

<强>更新

  

如果我想查找所有记录的联系人长度大于3

,该怎么办?
SELECT CASE
       WHEN NOT EXISTS(
         SELECT 1 
         FROM   Customers
         WHERE  LENGTH( ContactName ) <= 2
         OR     ContactName IS NULL
       )
       THEN 1
       ELSE 0
       END AS value
FROM   DUAL

SELECT CASE
       WHEN NOT EXISTS(
         SELECT 1 
         FROM   Customers
         WHERE  COALESCE( LENGTH( ContactName ), 0 ) <= 2
       )
       THEN 1
       ELSE 0
       END AS value
FROM   DUAL

注意:要使用索引,您需要在LENGTH( ContactName )COALESCE( LENGTH( ContactName ), 0 )上为相应的示例添加基于函数的索引。

答案 1 :(得分:2)

如果您的SQL检查“customers表中是否存在空联系人姓名”,那么您可以使用下面的oracle查询。

SELECT DECODE(cnt, 0, 0, 1)
  FROM (SELECT COUNT(1) cnt
          FROM customers
         WHERE NOT NVL(LENGTH(contact_name), 0) > 0);

SELECT DECODE(cnt, 0, 0, 1)
  FROM (SELECT COUNT(1) cnt
          FROM customers
         WHERE contact_name IS NULL);

答案 2 :(得分:0)

避免空错误使用nvl函数:

SELECT
  CASE
    WHEN (NOT EXISTS
      (SELECT NULL FROM Customers t0 WHERE NOT NVL(LENGTH(t0.ContactName),0) > 0
      ))
    THEN 1
    ELSE 0
  END val
FROM DUAL;

答案 3 :(得分:-1)

我得到了这样的话:

SELECT CASE WHEN (NOT EXISTS(SELECT NULL FROM Customers  t0 WHERE NOT 
    LENGTH(t0.ContactName) > 0)) THEN 1 ELSE 0 END val
FROM DUAL

感谢所有