0

Question

How could I change the following code from SGC1-0001[ to SGC1-001.

The table is stations with column code. I need to use an update query, but not sure how to use it.

There are more records in the column that differ slightly but I should be able to work it out with an example to do one.

Sample Data

I would like to do them in chunks though not one at a time.

SGC1-0001[
SGC1-0002[
..
SGC1-0019[
SGC1-0021[
SGC2-0001[
SGC2-0002[
..
SGC2-0016[
SGC2-0017[
SGC3-0003[
SGC3-0004[
...
SGC4-0018[
SGC4-0021[
SGC4-0022[
SGC4-0025[
SGC4-0029[

Logic (pseudo code)

Delete first 0 and last [ from all
10
  • 1
    Can you provide other starting and desired ending values? From what you've provided, we don't know if the solution you need must be dynamic or not. Commented Jan 7, 2015 at 16:40
  • SGC4-0022[ should be replaced by ? Commented Jan 7, 2015 at 16:47
  • Thanks for adding additional example starting values. What would the desired ending value for SGC4-0029[ be, for example? Commented Jan 7, 2015 at 16:47
  • Basically it will be string manipulation using SQL REPLACE and then whichever ones you want to do, use the WHERE clause to only do those. Commented Jan 7, 2015 at 17:02
  • 1
    @Dan Cundy:- see the my anwser, as it will delete the last '[' and first 0 after '-' from all records Commented Jan 7, 2015 at 17:03

2 Answers 2

3

If you're wanting to trim the last character of all records in that column as well as remove the leading 0, the following should do the trick:

UPDATE stations 
SET    code = REPLACE(LEFT(code, LEN(code) - 1), '-0', '-')

If you're just wanting to replace the leading 0, following should do:

UPDATE stations 
SET    code = REPLACE(code, '-0', '-')

Also, the following may be of interest:

String Functions

Sign up to request clarification or add additional context in comments.

5 Comments

@DanCundy sure--revised!
And I have now removed the ] off them all so this query won't work any more. If you could explain it I could maybe edit it.
@DanCundy the REPLACE function 'Replaces all occurrences of a specified string value with another string value.' So in this case replace is replacing all occurances of '-0' with '-' in the string which was passe (in this case 'LEFT(code, LEN(code) - 1)', which is essentially the record without the trailing ']'. Hope this helps! msdn.microsoft.com/en-us/library/ms186862.aspx
That makes sense, I was getting confused thinking they were 'minus 0'. Thank you very much for your help.
Cool, and gotcha sir. Welcome!
0

Try This, Update query

Because all your code have '[' at last then you can use this query. it will delete the last '[' and first 0 after '-' from all records

UPDATE stations
SET  CODE = LEFT(replace(code,right(code,1),''),4) + '-'+
            RIGHT(RIGHT(replace(code,right(code,1),''),4),3)

It will replace SGC1-0029[ to SGC1-029 AND SGC1-0001[ to SGC1-001

Better to refer some sql basic tutorials

Comments

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.