Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Determine if String has numbers in datatype varchar(10)


Determine if String has numbers in datatype varchar(10)

Author
Message
//Ravi
//Ravi
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 451
I am trying to clean up our Zip Code table. Most of the time the zip code entry is Zip+4. I have to clean up so that I am only keeping first 5 of the zip. Problem is that many times zip code column also has letter for the applicants outside of the country, so I can't modify them. I am only allowed to clean up zip+4. The logic I am using right now is

update DimStudent
set StudentZip = substring(StudentZip, 1, 5)
where len(StudentZip) = 10 and substring(StudentZip, 6, 1) = '-'

Problem with this code is that StudentZip doesn't always have Zip+4 (10 characters) and sometimes they don't have a hyphen. Sometimes it has Zip+3 or Zip+2 (I know, weird).

So I am told that the logic I should be using is: Check if the first first 5 characters are numbers. If they are numbers, then keep just first 5 charaters (or delete everything after 5 characters).

I checked many website and two books on T-SQL but couldn't find a code that will check to see if a string is number. Please help.

Thanks in Advance.

------------
Smile
Madhivanan-208264
Madhivanan-208264
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 476
Try

update DimStudent
set StudentZip = substring(StudentZip, 1, 5)
where StudentZip like '[0-9][0-9][0-9][0-9][0-9]%'



Madhivanan

Failing to plan is Planning to fail
GSquared
GSquared
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16179 Visits: 9729

Where MyZipColumn like '[0-9][0-9][0-9][0-9][0-9]%'



Will find anything that is five numbers, followed by anything or nothing.


Where MyZipColumn like '[0-9][0-9][0-9][0-9][0-9]'
or MyZipColumn like '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
or MyZipColumn like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'



Will find anything that's five numbers and nothing else, or anything that's five numbers followed by a hyphen and then four numbers, or anything that's nine numbers (five and four) without a hyphen.

The braces with a range in them select anything that matches that range. In this case, any single digit that's between 0 and 9.

Does that help?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
andrewd.smith
andrewd.smith
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1290 Visits: 3232
It goes LIKE this:

SELECT 
StudentZip,
SUBSTRING(StudentZip, 1, 5) AS CleanedZip
FROM DimStudents
WHERE (StudentZip LIKE '[0-9][0-9][0-9][0-9][0-9]%')


//Ravi
//Ravi
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 451
Thanks a lot guys. It worked.

------------
Smile
Madhivanan-208264
Madhivanan-208264
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 476
Rav (3/9/2009)
Thanks a lot guys. It worked.

You are welcome Smile



Madhivanan

Failing to plan is Planning to fail
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search