0

I have column with data like:

'2020193'
'3208391'
'1038291'
'9349203'

The data type is varchar and I can't change it to int (data managed in this datatype always).

I have some rows with trailing spaces like:

' 2222928'
' 3213331'

I need to remove that trailing space from start. I have tried SUBSTRING() or TRIM()/RTRIM()/LTRIM(), but didn't worked any of those.

select (rtrim(ltrim(doc_id))) from bpm.sales where len(doc_id) = 8
select left(doc_id,2) from bpm.sales where len(doc_id) = 8
select charindex(' ',doc_id) from bpm.sales where len(doc_id) = 8

Also, when I am trying to search the data like:

select doc_id from bpm.sales where doc_id = ' 2269203'

I am geting nothing where it exist in the column. With CHARINDEX() I got 0.

Can someone explain me this behaviour and suggest a solution?

Zhorov
  • 28,486
  • 6
  • 27
  • 52

1 Answers1

0

You can get rid of everything up to the first character you do want:

select stuff(doc_id, 1, patindex('%[^0-9a-zA-Z]%', doc_id) - 1, '')
        
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786