July 17, 2007 at 2:35 am
Oops! Did the work last night, posted this morning, sorry. You have a knack for posting the perfect solution Peter, magnificent. The most concise and the fastest method, and exactly according to the spec.
Cheers
ChrisM
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
July 17, 2007 at 4:49 am
Also without isnumeric() function
declare @Temp table (
Data nvarchar(14)
)
insert into @Temp
select '00000000000002'
insert into @Temp
select '00000000000100'
insert into @Temp
select '00000000002100'
insert into @Temp
select '000000000A4504'
select
replace(ltrim(replace(data,'0',' ')),' ','0')
from @Temp
July 17, 2007 at 5:57 am
Heh... even better... nicely done!
--Jeff Moden
Change is inevitable... Change for the better is not.
July 17, 2007 at 5:59 am
Heh... I jumped the gun a bit... Oleg's code seems to do the trick nicely, though.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 17, 2007 at 6:11 am
I am not sure
declare @Temp table (
Data nvarchar(14)
)
insert into @Temp
select '00000000000000'
insert into @Temp
select '00000000000002'
insert into @Temp
select '00000000000100'
insert into @Temp
select '00000000002100'
insert into @Temp
select '000000000A4504'
select
replace(ltrim(replace(data,'0',' ')),' ','0')
from @Temp
where data != '00000000000000'
union all
select
data
from @Temp
where data = '00000000000000'
July 17, 2007 at 6:32 am
data OlegS Peso
-------------- -------------- --------------
00 00
00000000000000 0000000000000
00000000000002 2 2
00000000000100 100 100
00000000002100 2100 2100
000000000A4504 A4504 A4504
N 56°04'39.16"
E 12°55'05.25"
July 17, 2007 at 6:49 am
declare @Temp table (
Data nvarchar(14)
)
insert into @Temp
select '00000000000000'
insert into @Temp
select '000000'
insert into @Temp
select '00'
insert into @Temp
select '00000000000002'
insert into @Temp
select '00000000000100'
insert into @Temp
select '00000000002100'
insert into @Temp
select '000000000A4504'
select
case
when len(replace(data,'0',' ')) = 0 then data
else replace(ltrim(replace(data,'0',' ')),' ','0')
end
from @Temp
July 17, 2007 at 7:22 am
Oleg, I do not know which is right. I just posted the both approaches for comparison.
The only one qualified to determine the right approach is original poster.
N 56°04'39.16"
E 12°55'05.25"
July 19, 2007 at 7:30 am
Thanks again!
CSDunn
July 19, 2007 at 5:42 pm
You're welcome, but we sure would like to know what you want done if the entry contains all zeros
--Jeff Moden
Change is inevitable... Change for the better is not.
July 20, 2007 at 7:06 am
Notwithstanding the answer to the 'all zero' question
if emptry string is required then
LTRIM(SUBSTRING([column], PATINDEX('%[^0]%', [column]+' '), LEN([column])))
Far away is close at hand in the images of elsewhere.
Anon.
July 20, 2007 at 9:32 am
In light of the unanswered "edge" conditions how about this?
Use TempDB
If Object_Id('dbo.test') is not Null Drop table dbo.Test
Select '123'[CharNum] into dbo.Test
Union All Select '0123'
Union All Select '000123'
Union All Select '000'
Union All Select 'XYZ'
Union All Select ''
Union All Select '0'
Select IsNull(Stuff(CharNum,1,i-1,''),'0')[NormalizedCharNum]
from (Select CharNum,PatIndex('%[1-9a-z]%',CharNum) from dbo.Test) t
(Adjust the PatIndex pattern for your requirements and collation)
July 20, 2007 at 4:57 pm
Guess I don't understand... Oleg's code seemed to work fine for all conditions according to what the OP wanted... here's the code again...
select
replace(ltrim(replace(data,'0',' ')),' ','0')
from @Temp
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 13 posts - 16 through 27 (of 27 total)
You must be logged in to reply to this topic. Login to reply