选择小于10的两列字符

时间:2019-06-12 07:28:35

标签: sql oracle oracle11g

我只希望下表中长度firstname+ lastname小于10的那些记录

CREATE TABLE table_name ( id, firstname, lastname ) AS
SELECT 1, 'ravi',         'kamble' FROM DUAL UNION ALL
SELECT 2, 'pravinkumar',  'kamble' FROM DUAL UNION ALL
SELECT 3, 'sunil',        'dev'    FROM DUAL UNION ALL
SELECT 4, 'rahul',        'k'      FROM DUAL UNION ALL
SELECT 5, 'abhijit',      's'      FROM DUAL UNION ALL
SELECT 6, 'kapil',        'dev'    FROM DUAL

输出

ID   firstname  lastname
3    sunil      dev     (8) 
4    rahul      k       (6) 
5    abhijit    s       (8) 
6    kapil      dev     (8) 

由于该记录的长度等于或大于10,因此该输出不包含记录1和2。

2 个答案:

答案 0 :(得分:2)

以下查询应执行您想要的操作:

SELECT ID, FirstName, LastName FROM TABLE
WHERE LENGTH(CONCAT(FirstName,LastName)) < 10

答案 1 :(得分:1)

您可以使用:

SELECT *
FROM   table_name
WHERE  LENGTH( firstname ) + LENGTH( lastname ) < 10

输出

ID | FIRSTNAME | LASTNAME
-: | :-------- | :-------
 3 | sunil     | dev     
 4 | rahul     | k       
 5 | abhijit   | s       
 6 | kapil     | dev     

db <>提琴here