July 7, 2014 at 7:57 pm
Hi Guys
I need a bit of help.
I have a column with integer records of 5 and 6 characters.
example below od 2 records.
12345
123456
What I need to do is
1. If its only 5 digits long....insert a leading zero.
2.Once all characters are 6 digits long...insert a dash between them.
So my 2 records above should end up looking like this.
012-345
123-456
Any assistance will be appreciated.
A
July 7, 2014 at 8:21 pm
C-kweldude (7/7/2014)
Hi GuysI need a bit of help.
I have a column with integer records of 5 and 6 characters.
example below od 2 records.
12345
123456
What I need to do is
1. If its only 5 digits long....insert a leading zero.
2.Once all characters are 6 digits long...insert a dash between them.
So my 2 records above should end up looking like this.
012-345
123-456
Any assistance will be appreciated.
A
Easy, this:
declare @test-2 varchar(10) = '12345';
select stuff(right('000000' + @test-2, 6),4,0,'-')
July 7, 2014 at 8:35 pm
Hi Lynn
Sorry, still a bit vague for me.
The table name is Branch and the column is called branchcode.
It should be some kind of update statement?
Thanks
A
July 7, 2014 at 8:57 pm
C-kweldude (7/7/2014)
Hi LynnSorry, still a bit vague for me.
The table name is Branch and the column is called branchcode.
It should be some kind of update statement?
Thanks
A
This help any?? By the way, Books Online is your friend and can explain all of this.
create table dbo.test(
testid int identity(1,1),
MyValue varchar(10));
insert into dbo.test(MyValue)
values ('12345'),('123456'),('5678'),('56789'),('567890');
select testid, MyValue from dbo.test;
select testid, MyValue, stuff(right('000000' + MyValue, 6),4,0,'-') from dbo.test;
update dbo.test set
MyValue = stuff(right('000000' + MyValue, 6),4,0,'-');
select testid, MyValue from dbo.test;
go
drop table dbo.test;
go
July 7, 2014 at 9:04 pm
Also, if you update the values in place, look what happens if you run the STUFF(RIGHT...)) against the modified values:
create table dbo.test(
testid int identity(1,1),
MyValue varchar(10));
insert into dbo.test(MyValue)
values ('12345'),('123456'),('5678'),('56789'),('567890');
select testid, MyValue from dbo.test;
select testid, MyValue, stuff(right('000000' + MyValue, 6),4,0,'-') from dbo.test;
update dbo.test set
MyValue = stuff(right('000000' + MyValue, 6),4,0,'-');
select testid, MyValue from dbo.test;
select testid, MyValue, stuff(right('000000' + MyValue, 6),4,0,'-') from dbo.test;
go
drop table dbo.test;
go
July 7, 2014 at 9:32 pm
Hi Lynn
Awesome!:-)
Cheers for the help and the links...ill check them out.
A
July 8, 2014 at 1:59 am
C-kweldude (7/7/2014)
...I have a column with integer records of 5 and 6 characters....
What datatype is the column? It's not clear if you are describing the datatype or the content.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 8, 2014 at 3:16 pm
ChrisM makes a good point as a numeric column will not give the expected results...
Either the column is a char/varchar or will have to be cast at select time because if we take
select '0'+12
it will upconvert to '0' to 0 and add it to 1.
Thus if the column is numeric...
(If I can borrow a piece from Lynn's code) >
select testid, MyValue, stuff(right('000000' + CAST(MyValue as varchar(10)), 6),4,0,'-') from dbo.test
should work.
----------------------------------------------------
October 8, 2014 at 3:35 pm
MMartin1 (10/8/2014)
ChrisM makes a good point as a numeric column will not give the expected results...Either the column is a char/varchar or will have to be cast at select time because if we take
select '0'+12
it will upconvert to '0' to 0 and add it to 1.
Thus if the column is numeric...
(If I can borrow a piece from Lynn's code) >
select testid, MyValue, stuff(right('000000' + CAST(MyValue as varchar(10)), 6),4,0,'-') from dbo.test
should work.
This would be even better for integers:
select testid, MyValue, stuff(right(1000000 + MyValue, 6),4,0,'-')
from dbo.test
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply