0

I have a column called item and I would like to add whitespace at the START of each row in the item column.

I would also like to remove the 3 and insert the result into a new column called item_n.

item                 item_n
-------------------|-----------
to go 3            |
1 2 3              |
cat dog 3          |
blah blah 3

RESULT:

item          |  item_n
------------------------------
to go 3       | to go
1 2 3         | 1 2
cat dog 3     | cat dog
blah blah 3   | blah blah

Thanks in advance

2
  • Edit your question and show the results that you want. Commented Mar 5, 2018 at 2:45
  • Your title and your sample data don't really make sense. Commented Mar 5, 2018 at 2:54

2 Answers 2

1

Try this it will give you the perfect answer

Select concat(' ', replace(item,' 3',' ')) from table name
Sign up to request clarification or add additional context in comments.

Comments

0

You can just use left() with a negative second argument:

select left(item, -2) as item_n

If you know that the " 3" only appears at the end of the string, you can do:

select replace(item, ' 3', '')

Or, if the " 3" might not be in the string, then case is one solution:

select (case when item like '% 3' then left(item, -2) else item
        end) as item_n

You can prepend white space by using string concatenation:

select ' ' || left(item, -2) as item_n

5 Comments

Thanks, how do I create the whitespace at the start of all rows in item_n?
@Paul . . . You can concatenate spaces, but I don't understand what you mean.
I know it's hard to see from my example but in the item_n column in the results there is whitespace at the front
@GordonLinoff Nice answer with different solutions . Thank you
Thanks for you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.