Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Couple of update statements

0 views
Skip to first unread message

Rob

unread,
Jun 5, 2007, 9:08:24 PM6/5/07
to
How would you wrte the following update statements ?

Update a column that strips leading zeros

Update a column that strips any trailing Alpha characters

Thanks,
Rob


--CELKO--

unread,
Jun 5, 2007, 9:53:06 PM6/5/07
to
>> How would you write the following update statements ? <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

Why do you have such strings in the Schema when you can write CHECK()
constraints that would prevent this in the first place?? That is one
of the basic ideas of RDBMS.

Erland Sommarskog

unread,
Jun 5, 2007, 10:12:04 PM6/5/07
to
Rob (ro...@yahoo.com) writes:
> How would you wrte the following update statements ?
>
> Update a column that strips leading zeros

substring(col, patindex('%[^0]%', col) - 1, len(col)



> Update a column that strips any trailing Alpha characters

substring(col, 1, len (col) - (patindex('%[^A-z]%', reverse(col) - 1)))

Both these are untested, and there may be one-off errors. Also, the
exact expression for trailing alpha is collation dependent. The above
would not be correct for Swedish text if there is ÅÄÖ in it.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Rob

unread,
Jun 5, 2007, 11:54:39 PM6/5/07
to
Regarding...

>Why do you have such strings in the Schema when you can write CHECK()
>constraints that would prevent this in the first place?? That is one
>of the basic ideas of RDBMS

That is a good question for the shipping company that has exported this data
into a spreadsheet from their system and forwarded it to us.

Regarding "Please post DDL" , I thought it was somewhat of a generic
request, but I do agree with you.


"--CELKO--" <jcel...@earthlink.net> wrote in message
news:1181080386....@q66g2000hsg.googlegroups.com...

Rob

unread,
Jun 5, 2007, 11:55:28 PM6/5/07
to
Thank you very much, I will give them a try...


"Erland Sommarskog" <esq...@sommarskog.se> wrote in message
news:Xns99472380...@127.0.0.1...


> Rob (ro...@yahoo.com) writes:
>> How would you wrte the following update statements ?
>>
>> Update a column that strips leading zeros
>
> substring(col, patindex('%[^0]%', col) - 1, len(col)
>
>> Update a column that strips any trailing Alpha characters
>
> substring(col, 1, len (col) - (patindex('%[^A-z]%', reverse(col) - 1)))
>
> Both these are untested, and there may be one-off errors. Also, the
> exact expression for trailing alpha is collation dependent. The above

> would not be correct for Swedish text if there is ЕДЦ in it.

Rob

unread,
Jun 6, 2007, 2:15:38 AM6/6/07
to
Hi,

I did try the following (to get rid of the leading zeros), but it does not
appear to be working... maybe I did something wrong...

CREATE TABLE [dbo].[LTLData](
[ProNum] [nvarchar](255) COLLATE Latin1_General_BIN NULL
) ON [PRIMARY]

insert into LTLData (ProNum) Values ('00234')
insert into LTLData (ProNum) Values ('030234BD')
insert into LTLData (ProNum) Values ('33030234BD')
insert into LTLData (ProNum) Values ('0233030234BD')
insert into LTLData (ProNum) Values ('0000234BD')

update LTLData set ProNum =substring(ProNum, patindex('%[^0]%', ProNum) - 1,
len(ProNum))


Thanks,
Rob


"Erland Sommarskog" <esq...@sommarskog.se> wrote in message
news:Xns99472380...@127.0.0.1...

> Rob (ro...@yahoo.com) writes:
>> How would you wrte the following update statements ?
>>
>> Update a column that strips leading zeros
>
> substring(col, patindex('%[^0]%', col) - 1, len(col)
>
>> Update a column that strips any trailing Alpha characters
>
> substring(col, 1, len (col) - (patindex('%[^A-z]%', reverse(col) - 1)))
>
> Both these are untested, and there may be one-off errors. Also, the
> exact expression for trailing alpha is collation dependent. The above

> would not be correct for Swedish text if there is ЕДЦ in it.

Uri Dimant

unread,
Jun 6, 2007, 5:45:20 AM6/6/07
to
Rob
If you use SQL Server 2005 that is a perfect chance using CLR to do the job

--T-SQL

while exists (select * from LTLData where left(ProNum,1) like '0%')
update LTLData
set ProNum = stuff(ProNum, patindex('0%', ProNum), 1, space(0))
where left(ProNum,1) like '0%'


"Rob" <ro...@yahoo.com> wrote in message
news:_6mdnccFaaimhPvb...@comcast.com...

Rob

unread,
Jun 6, 2007, 10:36:26 AM6/6/07
to
Thanks Uri,

How might a statement be constructed to trim off trailing alpha characters
(same data) ?

CREATE TABLE [dbo].[LTLData](
[ProNum] [nvarchar](255) COLLATE Latin1_General_BIN NULL
) ON [PRIMARY]

insert into LTLData (ProNum) Values ('00234')
insert into LTLData (ProNum) Values ('030234BD')
insert into LTLData (ProNum) Values ('33030234BD')
insert into LTLData (ProNum) Values ('0233030234BD')
insert into LTLData (ProNum) Values ('0000234BD')

Rob


"Uri Dimant" <ur...@iscar.co.il> wrote in message
news:%230k%23Y4$pHHA...@TK2MSFTNGP05.phx.gbl...

Rob

unread,
Jun 6, 2007, 10:57:41 AM6/6/07
to
Uri,

A question about this...

I see that the statement contains a "while" thus I assume it is some kind of
"loop". For my purpose, it is fine, however, knowing the disdain some
folks have for cursors... where does CLR stand ? In other words, which
would be better to use.... CLR or a cursor ?

Also, what is a good source for learning how to use CLR within SQL2005 ?

Thanks,
Rob

"Uri Dimant" <ur...@iscar.co.il> wrote in message
news:%230k%23Y4$pHHA...@TK2MSFTNGP05.phx.gbl...

Erland Sommarskog

unread,
Jun 6, 2007, 10:33:08 PM6/6/07
to
Rob (ro...@yahoo.com) writes:
> I did try the following (to get rid of the leading zeros), but it does not
> appear to be working... maybe I did something wrong...

I did say they were sketches, and I hope you would be able to work
with them as a start. If you look at the result from the first query:



> update LTLData set ProNum =substring(ProNum, patindex('%[^0]%', ProNum) -
> 1, len(ProNum))

We see that one 0 is retained. It appears that dropping the -1 resolves
the problem. (Patindex is quite confusing, so I was on a dare when I
posted without testing. But without test data....

The other query I posted gives an error, because of a misplaced right
parenthesis. The correct query reads:

substring(ProNum, 1,
len(ProNum) - (patindex('%[^A-z]%', reverse(ProNum)) - 1))

And when I test with the data you posted, it appears to give the desired
result.

Rob

unread,
Jun 7, 2007, 11:25:46 AM6/7/07
to
Thank You Very Much !


"Erland Sommarskog" <esq...@sommarskog.se> wrote in message

news:Xns99485CCA...@127.0.0.1...

Steve Dassin

unread,
Jun 8, 2007, 2:53:56 AM6/8/07
to
Here is an easy approach for application developers to steam clean strings:

http://beyondsql.blogspot.com/2007/06/dataphor-trimming-digits-and-letters.html


0 new messages