I have the following sql query for transforming data but is it possible to save the value of the int in some variable to avoid casting multiple times?
update prospekts set sni_kod = case when cast(sni_kod as int) >= 1000 and cast(sni_kod as int) <= 1499 or cast(sni_kod as int) >= 1600 and cast(sni_kod as int) <= 2439 then '1' when cast(sni_kod as int) >= 7000 and cast(sni_kod as int) <= 7499 then 'W' else sni_kod end
There are a lot more when-cases in the script, just showing the first one. I cannot use anything other than a simple text-script.
Update Using SQL Server 2000
Thanks
Anders
Ok… here’s my rewrite of your code…
This way, it’ll only attempt to do a CAST if it’s a numeric value, so you won’t get cast exceptions, like other people have mentioned in comments.
Since you said there are a lot more statements involved, I’m guessing you have a lot more number ranges that get different values… If that’s the case, you might be able to use a second table (can be a temporary one if, like your question says, you’re limited to just SQL code) to join on which have min value, max value, and what you want to display based on that. Gets more tricky when you need to evaluate non-numeric values, but it isn’t impossible.
Without seeing the full statement, though, this is the best I can offer.