Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Faster alternatives to CAST

Author  Topic 

Bertie
Starting Member

7 Posts

Posted - 2012-06-11 : 14:47:01
Hello,

I'm writing a query within which I have to use MIN(SomeValue). The problem I have is that SomeValue is a number stored as a varchar, so it is evaluating incorrectly in some cases - for example 250 is taken as higher than 12345 due to the first number. What I've done to get round this limitation is put MIN(CAST(SomeValue) as INT)), however it's completely killed the query. Before it would take 45 seconds to return 80,000 lines, however now it returns 800 in about 4 minutes! Can anyone please suggest a better way for me to do this if possible?

Thanks!

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-06-11 : 14:55:31
The short answer is, there isn't. The longer answer is to use proper datatypes so you don't have to CAST/CONVERT.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-11 : 15:26:52
if its storing integer values one best thing is to change datatype in table field itself so that there's no need to do casting as Lamprey suggested

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Bertie
Starting Member

7 Posts

Posted - 2012-06-11 : 16:58:28
quote:
Originally posted by Lamprey

The short answer is, there isn't. The longer answer is to use proper datatypes so you don't have to CAST/CONVERT.



Sadly not my original data! But thanks for the replies
Go to Top of Page
   

- Advertisement -