查询一列数字字段的前3位数字

时间:2018-07-30 15:00:32

标签: sql oracle oracle11g

我有一张桌子,国家:

loctn | area  | people
abc   | 12345 | 153465
cxv   | 43566 | 388573

我正在寻找选择查询的输出,例如:

select area, people from Country where loctn='abc' 

loctn | area | people
abc   | 123  | 153

2 个答案:

答案 0 :(得分:2)

根据您的评论,您实际上确实希望将其除以1000。您可以截断或获得下限值,因此它是一个整数-例如floor(area/1000)-然后根据需要连接固定的字符串值。

-- sample data as a CTE
with country (loctn, area, people) as (
  select 'abc', 12345, 153465 from dual
  union all select 'cxv', 43566, 388573 from dual
  union all select 'xyz', 134567 , 1234567  from dual
)
-- actual query
select loctn,
  floor(area/1000) || 'K+' as area,
  floor(people/1000) || 'K+' as people
from country;

LOCTN AREA       PEOPLE    
----- ---------- ----------
abc   12K+       153K+     
cxv   43K+       388K+     
xyz   134K+      1234K+     

或对于单个值,例如您的问题中的查询:

select loctn,
  floor(area/1000) || 'K+' as area,
  floor(people/1000) || 'K+' as people
from country
where loctn = 'abc';

LOCTN AREA       PEOPLE    
----- ---------- ----------
abc   12K+       153K+     

这与您在注释中描述的输出(我认为)相匹配,而不是与问题中显示的内容相匹配,但仍不清楚您真正想要的是什么。

答案 1 :(得分:0)

面积和人数数字字段吗?

如果是,请尝试在下面查询。否则,从查询中删除to_char并运行。

select loctn,
       substr(to_char(area),1,3) as area,
       substr(to_char(people),1,3)  as people 
from Country