September 1, 2006 at 10:43 am
I'm hoping someone can give me some direction on this.
The ERP Database we have contains a field named 'ShipToNum' that stores Ship ID's but it is a char data type. The issue I'm having is that there are certain records that contain an ID that might look like 'hug1' but the ID is supposed to be a number like 12.
I need to return records that only contain a "Real" Ship ID.
I've been doing a bunch of searches to find an answer but haven't come across anything yet. I thought I could use Contains() but it doesn't seem to work. And WildCard searches won't work either.
I would use the IsNumeric() Function but because the data type is 'char' it always returns false.
Does anyone have any good ideas on this?
Thanks,
Ben
September 1, 2006 at 11:00 am
select *
from YourTable
where ShipToNum not like '%[^0-9]%'
September 1, 2006 at 11:01 am
Do you know what the width of the Number is supposed to be (for example last 3 Charaters are ID etc)?
Does it have a specific character that seperates Characters and Number?
If you have this then its an easy query.
Thanks
Sreejith
September 1, 2006 at 11:02 am
This works for me... maybe you can expand from that :
select
isnumeric(rtrim(ltrim('hug1 ')))
--0
select isnumeric(rtrim(ltrim('1 ')))
--1
September 1, 2006 at 11:14 am
unfortunately there isn't any type of consistency with the way the characters are stored.
so they could be 'hug1' '1hug' '1hu2g' etc...
September 1, 2006 at 11:15 am
Ben,
Try this.
Declare @s_Data varchar(50),
@s_ID varchar(50)
set @s_Data = '5ABC1238990'
set @s_ID =''
select @s_Data
If IsNumeric(Right(@s_Data,1)) =1
Begin
while IsNumeric(Right(@s_Data,1)) =1
Begin
set @s_ID = Cast(Right(@s_Data,1) as char(1)) + @s_ID
set @s_Data = left(@s_Data,len(@s_Data)-1)
End
select @s_Data,@s_ID
End
If this works create this as UDF and you can use it.
Thanks
Sreejith
September 1, 2006 at 11:24 am
What's wrong with the solution I provided?
What else do you need to do besides checking if it's a real id?
Do you need to extract it to another field, update the data??
September 1, 2006 at 11:48 am
RGR'us, unfortunately isNumeric() doesn't seem to work.
Our ERP system actually uses a progress database but it's supposed to be SQL-92 compliant. Most functions work as they would in SQL Server but for some reason isNumeric() throws an error.
September 1, 2006 at 11:55 am
What error does it throw?
September 1, 2006 at 12:03 pm
Just gives a syntax error. Even doing this will give that error:
SELECT IsNumeric(ShipToNum)
From Pub.ShipTo
Where CustNum = 10
September 1, 2006 at 12:09 pm
Ok, can't help you much there... works fine on sql server.
September 2, 2006 at 10:17 pm
Don't forget that ISNUMERIC sometimes allows an "e" or "d" and will also allow decimal points and dollar signs and etc, etc....
The method Ken uses is nasty fast and is guaranteed to find only those things that have all digits... you can trim to keep leading spaces from being a problem (as you did).
[Edit] Ken's solution should also work fine in just about any RDBMS that allows rational expressions in LIKE clauses....
--Jeff Moden
Change is inevitable... Change for the better is not.
September 3, 2006 at 5:18 am
Thanx for the info... I didn't know that about the IsNumeric Function .
September 3, 2006 at 11:37 am
That's one of the things I admire about you, Remi... absolute integrity and honesty...
Here's a couple of links I was involved with that explain it all...
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=243646&p=2
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1073&lngWId=5
--Jeff Moden
Change is inevitable... Change for the better is not.
September 3, 2006 at 6:15 pm
Thanx for the links. I had already met part of the problem with strings like ',' or ',' but that's about it. That would be a nice gotcha to put in the QOD... I'm curious to see how many DBA can get that answer right without running the code in QA .
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply