0
               I have table following this format :

Id   BillNo:      Voucher No:
1   W2015-16/0001  W2015-16/0001
2   W2015-16/0002  W2015-16/0002
3   W2015-16/0003  W2015-16/0003
4   W2015-16/0004  W2015-16/0004
5   W2015-16/0005  W2015-16/0005
6   W2015-16/0006  W2015-16/0006
7   W2015-16/0007  W2015-16/0007
8   W2015-16/0008  W2015-16/0008
9   W2015-16/0009 W2015-16/0009
10   W2015-16/0010  W2015-16/0010

But Instead of this Format: Now I want to Update All "Voucher No" and "Bill No" below on this format :

Id   BillNo:      Voucher No:
1   W0001/2015-16 W0001/2015-16
2   W0002/2015-16 W0002/2015-16
3   W0003/2015-16 W0003/2015-16
4   W0004/2015-16 W0004/2015-16
5   W0005/2015-16 W0005/2015-16
6   W0006/2015-16 W0006/2015-16
7   W0007/2015-16 W0007/2015-16
8   W0008/2015-16 W0008/2015-16
9   W0009/2015-16 W0009/2015-16
10  W0010/2015-16 W0010/2015-16
11  W0011/2015-16 W0011/2015-16

like this i have a 1000+ records i should update , But i don't know is this possible to do in Lesser time, Kindly give your suggestion , i am new to SQL Thanks Advance

3
  • please don't tag irrelevant products. how is mysql and oracle used here? Commented May 26, 2015 at 11:35
  • Question -> Only First CHAR is always a constant value or can expect more values in first place? W2015 .. can it be WER2015? Commented May 26, 2015 at 11:45
  • only first char is constant , cannot modified @user2066540 Commented May 27, 2015 at 11:59

3 Answers 3

1

Assuming everything is in a fixed format, then you would just use update and string manipulations:

update table t
    set billno = left(billno, 1) + right(billno, 4) + '/' + substring(billno, 2, 7),
        VoucherNo = left(VoucherNo, 1) + right(VoucherNo, 4) + '/' + substring(VoucherNo, 2, 7);
Sign up to request clarification or add additional context in comments.

Comments

0
UPDATE YourTable
SET BillNo = SUBSTRING(BillNo,1,1) + SUBSTRING(BillNo,10,4) + '/' + SUBSTRING(BillNo,2,7)
, VoucherNo = SUBSTRING(VoucherNo,1,1) + SUBSTRING(VoucherNo,10,4) + '/' + SUBSTRING(VoucherNo,2,7)

Test:

DECLARE @a NVARCHAR(max) = 'W2015-16/0001' 
SELECT SUBSTRING(@a,1,1) +
SUBSTRING(@a,10,4) + '/' + SUBSTRING(@a,2,7)

3 Comments

It's Awesome ,, Really very thank full, Giving Ur Code is Working Fine and greatful @Nenad Zivkovic
Thanks for All helping my problem ;)
@FelishAnand Sure, no problem: Substring function splits your string into parts. SUBSTRING(BillNo,1,1) takes 1 character from position 1, which is W in your example. SUBSTRING(BillNo,2,7) is 7 characters starting from position 2 = 2015-16, and SUBSTRING(BillNo,10,4) is 4 characters from 10th position which is 0001... So basically you split your input string into parts and reassemble it in different order.
0

There is no straight forward way to achieve this. You may need to define some patterns in SQL to replace and play with all string functions. Here is

declare @name varchar(50) ='W2015-16/0001';
select SUBSTRING(STUFF(@name,2,0,(SUBSTRING(@name,CHARINDEX('/',@name) + 1,len(@name)) + '/')),0,14)
--W0001/2015-16

This gives you the result that you are looking for. So you need to set this into your update statement by joining with the table by ID. This would work only if the pattern you have given is correct.

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.