Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
convert data type
15 posts, Page 1 of 2
1
2
»»
convert data type
Rate Topic
Display Mode
Topic Options
Author
Message
sqlfriends
sqlfriends
Posted Tuesday, November 27, 2012 4:11 PM
SSCommitted
Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 1,639,
Visits: 2,843
I would like to convert a column from varchar(10) to int.
I also want to ignore any values that have charaters in it.
I searched online and used this:
select RecordId, [name]
,Cast(BuildingCode as int) as BuildingCode
where BuildingCode Not LIKE '%[^0-9.-]%'
but I don't actually know what does this Not LIKE '%[^0-9.-]%' mean?
What ^ represent for?
Thanks
Post #1389455
Koen Verbeeck
Koen Verbeeck
Posted Wednesday, November 28, 2012 12:32 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 9,410,
Visits: 6,495
^ is the negation. So %[^0-9.-]% means "every character that is not 0 through 9, a dot or the minus symbol".
LIKE (Transact-SQL)
How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
Member of
LinkedIn
. My blog at
LessThanDot
.
MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1389539
WolfgangE
WolfgangE
Posted Wednesday, November 28, 2012 1:12 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Yesterday @ 11:51 PM
Points: 109,
Visits: 492
Here is a nice article which shows you why one would use this expression:
http://www.sqlservercentral.com/articles/IsNumeric/71512/
Post #1389552
subbareddy542
subbareddy542
Posted Wednesday, November 28, 2012 2:39 AM
SSC Eights!
Group: General Forum Members
Last Login: Thursday, June 13, 2013 12:35 AM
Points: 835,
Visits: 616
pls check below code:
declare @t1 table(id int,name varchar(10))
insert into @t1(id,name) values(1,'1'),(2,'2'),(3,'4'),(4,'7'),(5,'8_'),(9,'9'),(10,'0')
select id,CONVERT(int,replace(name,'_','')) from @t1
--CONVERT(int,replace(name,'_','')) from @t1
where name like '%[^0_9]%'
here name will come between 0 and 9 (zero,nine related records will not come)
Post #1389592
sqlfriends
sqlfriends
Posted Wednesday, November 28, 2012 10:10 AM
SSCommitted
Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 1,639,
Visits: 2,843
Koen Verbeeck (11/28/2012)
^ is the negation. So %[^0-9.-]% means "every character that is not 0 through 9, a dot or the minus symbol".
LIKE (Transact-SQL)
Thanks, and thanks for site link, it makes more sense to me now.
If this is the case, I think I don't need to include . and - sign.
Because my building code always is a number, no negative, no decimals.
So shall I just use
not like %[^0-9]%
Post #1389954
Koen Verbeeck
Koen Verbeeck
Posted Wednesday, November 28, 2012 10:25 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 9,410,
Visits: 6,495
sqlfriends (11/28/2012)
Koen Verbeeck (11/28/2012)
^ is the negation. So %[^0-9.-]% means "every character that is not 0 through 9, a dot or the minus symbol".
LIKE (Transact-SQL)
Thanks, and thanks for site link, it makes more sense to me now.
If this is the case, I think I don't need to include . and - sign.
Because my building code always is a number, no negative, no decimals.
So shall I just use
not like %[^0-9]%
This is actually a double negation, so the following should work as well:
LIKE '%[0-9]%'
Be aware that using the % symbol at the start of a LIKE clause won't have good performance, as an index can't be used.
How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
Member of
LinkedIn
. My blog at
LessThanDot
.
MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1389972
sqlfriends
sqlfriends
Posted Wednesday, November 28, 2012 10:34 AM
SSCommitted
Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 1,639,
Visits: 2,843
Thanks, that makes sense.
But I wonder why some people use : not like %[^0-9]%
For my case, can I just change it to
Like '[0-9]%'.
What is the difference between like '%[0-9]%'
and like '[0-9]%'.
Post #1389980
Koen Verbeeck
Koen Verbeeck
Posted Wednesday, November 28, 2012 10:38 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 9,410,
Visits: 6,495
There's no logical difference between NOT LIKE '%[^0-9]%' and LIKE '%[0-9]%'. Because the first one is a double negation, it will be the same when you leave all the negations out.
How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
Member of
LinkedIn
. My blog at
LessThanDot
.
MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1389985
sqlfriends
sqlfriends
Posted Wednesday, November 28, 2012 10:40 AM
SSCommitted
Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 1,639,
Visits: 2,843
Thanks,
how about the difference
between
like '%[0-9]%'
and like '[0-9]%'.
Post #1389988
Sean Lange
Sean Lange
Posted Wednesday, November 28, 2012 11:34 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Today @ 3:32 PM
Points: 8,980,
Visits: 8,540
sqlfriends (11/28/2012)
Thanks,
how about the difference
between
like '%[0-9]%'
and like '[0-9]%'.
Are you familiar with with using wildcard searches? The first one will find any row that contains a 0-9 anywhere in the value. The second one will find any row that starts with 0-9.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1390017
« Prev Topic
|
Next Topic »
15 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.