1

I have a column say column_1 and its values are:

abc 12edf
hbnm 847
47sf hg41

I need the output as follows:

abc
hbnm
47sf

PS: I have read only access to the db

leftjoin
  • 36,950
  • 8
  • 57
  • 116

1 Answers1

2

Use regexp_extract(col,'^(.*?)\\s',1) to extract everything from the beginning of the string before space (group 1) in the regexp.

'^(.*?)\\s' means:

^- the beginning of the string anchor (.*?) - any character any number of times \\s - space

Demo:
with your_table as (--use your_table instead of this
select stack (3,
'abc  12edf',
'hbnm 847',
'47sf hg41'
) as str
)

select regexp_extract (str,'^(.*?)\\s',1) as result_str 
  from your_table s

Result:

abc
hbnm
47sf

One more possible solution is using split:

select split (str,' ')[0] as result_str

And one more solution using instr+ substr:

select substr(str,1,instr(str,' ')-1)
leftjoin
  • 36,950
  • 8
  • 57
  • 116