Архив

Публикации с меткой ‘length’

Узнать является ли строка числом

17 Март 2010 3 comments

Запрос для вывода только числовых значений, используем REGEXP.

При необходимости, оставляем только точку или запятую ‘^-?[[:digit:].]*$‘ или ‘^-?[[:digit:],]*$‘.
Или только целые числа ‘^-?[[:digit:]]*$

with t as (
  select '56456456' str from dual
  union all
  select '4 5' str from dual
  union all
  select 'sds5' str from dual
  union all
  select '-1,5' str from dual  
  union all
  select '-1.5' str from dual  
  )
  select * FROM t 
	where regexp_like(str, '^-?[[:digit:].,]*$');

STR
--------
56456456
-1,5
-1.5

Ну и более простой вариант, который тоже подходит в некоторых случаях:

select case when length(TRIM(TRANSLATE('45', ' 0123456789', ' '))) is null then 'ЧИСЛО'
            else 'СТРОКА'
       end res from dual
union all
select case when length(TRIM(TRANSLATE('a4', ' 0123456789', ' '))) is null then 'ЧИСЛО'
            else 'СТРОКА'
       end res from dual

Вывести строки, которые начинаются на цифру

with t as (
  select '56456456' str from dual
  union all
  select '4 5' str from dual
  union all
  select 'sds5' str from dual
  union all
  select '-1,5' str from dual  
  union all
  select '-1.5' str from dual  
  )
  select * FROM t 
	where regexp_like(str, '^\d');

STR
--------
56456456
4 5