Groups | Blog | Home
all groups > sql server mseq > october 2006 >

sql server mseq : query


Tiffany
10/25/2006 9:57:01 PM
hi,

Have a problem and hope someone can help.

I have a column which consists of numbers such as
123
235
456
888
etc

How can i write a simple query to change them to
00123
00235
00456
00888
etc

Kindly advise.

Thank you very much
Warren Brunk
10/25/2006 10:08:44 PM
you can concatenate the data using a + sign.

Select '00' + cast(ColumnNameHere as varchar(255)) from tablename

--
/*
Warren Brunk - MCITP - SQL 2005, MCDBA
www.techintsolutions.com
Tech Blog - www.technologyis.com
*/


[quoted text, click to view]

Tiffany
10/25/2006 10:30:01 PM
Thank you very much. What if in the column, there is also alphabets like
abc
rde
fgd
123
256

and I just want 00+ to apply to numbers only. What should I do? Kindly advise.

Thank you

[quoted text, click to view]
Arnie Rowland
10/26/2006 1:41:15 PM
Hugo,=20

Instead of combining NOT LIKE and [^0-9], wouldn't that be easier to =
understand as:

SELECT CASE
WHEN ColumnNameHere LIKE '%[0-9]%' THEN '00'=20
ELSE ''=20
END + ColumnNameHere=20
FROM TableNameHere;

(I've always hated double negatives...)

--=20
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.=20
Most experience comes from bad judgment.=20
- Anonymous

You can't help someone get up a hill without getting a little closer to =
the top yourself.
- H. Norman Schwarzkopf


[quoted text, click to view]
Hugo Kornelis
10/26/2006 10:16:36 PM
[quoted text, click to view]

Hi Tiffany,

Use a CASE expression:

SELECT CASE
WHEN ColumnNameHere NOT LIKE '%[^0-9]%'
THEN '00' ELSE '' END + ColumnNameHere
FROM TableNameHere;

--
Hugo Kornelis
10/26/2006 11:54:11 PM
[quoted text, click to view]

Hi Arnie,

Easier to understand, but incorrect as well. Try it with '1s3' or 'af7'
as input data.

Double negatives are a bane for language, but a boon for SQL. Not only
for relational division, but also for constructing LIKE clauses. The
very limited regexp powers of LIKE don't allow for a test of "all
characters have to be numeric", but they do allow a test for "no
character may not be numeric".

Of course, if the length of the string is guaranteed to be always three
characters, you could also use LIKE '[0-9][0-9][0-9]'.

--
Arnie Rowland
10/27/2006 12:05:09 AM
I see your point Hugo.

Your illustration of using the double negative will cover all
possibilities -pure or mixed alphanumerics, whereas my method only covers
all alpha or all numeric -but not the mixed possibilities.

For the OP's situation as presented (all alpha or all numeric), either will
work, but your method is definitely more robust.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous

You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf


[quoted text, click to view]

Michael Abair
10/27/2006 9:44:03 AM
the isnumeric function is probably the most efficient way of accomplishing
this task

--
Michael Abair
Programmer / Analyst
Chicos FAS Inc.


[quoted text, click to view]

Hugo Kornelis
10/29/2006 12:19:25 AM
[quoted text, click to view]

Hi Michael,

Except that isnumeric returns 1 if the value passed to it can be
converted to at least one numeric datatype - not necessary int.

Try
SELECT ISNUMERIC('$'), ISNUMERIC('1E3'), ISNUMERIC('1.d2')

--
AddThis Social Bookmark Button