SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Optimized way to write
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sent_sara
Constraint Violating Yak Guru

India
370 Posts

Posted - 06/18/2013 :  14:10:34  Show Profile  Reply with Quote
Can i know how to write the optimized way in select statement,avoiding the case statment.


select 
CASE WHEN RACCT = '' OR RACCT IS NULL THEN 'No_Account' 
ELSE 'AC_' + RACCT + '' END  AS Account
From DBO.GL

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/18/2013 :  14:14:23  Show Profile  Reply with Quote
it can be simplified as below but internally its equivalent to CASE ...WHEN so cant say its optimized in performance

select COALESCE('AC_' + NULLIF(RACCT,'') + '','No_Account')  AS Account
From DBO.GL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3744 Posts

Posted - 06/18/2013 :  15:49:02  Show Profile  Reply with Quote
When you run a query, most of the resources are spent on retrieving the data, sorting it etc. Very little resources are required for arithmetic/logical operations as in the CASE expression you are using. So I would keep it as it is. It is more readable as well, just format it nicely
SELECT  
    CASE 
       WHEN RACCT = '' OR RACCT IS NULL THEN 'No_Account'
       ELSE 'AC_' + RACCT + ''
    END AS Account
FROM
    BO.GL
Go to Top of Page

nevzab
Starting Member

United Kingdom
34 Posts

Posted - 06/18/2013 :  16:33:14  Show Profile  Reply with Quote
Is this any better?

select

CASE WHEN isnull(RACCT,'') = '' THEN 'No_Account'
ELSE 'AC_' + RACCT + '' END AS Account
From DBO.GL
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

India
370 Posts

Posted - 06/18/2013 :  17:22:59  Show Profile  Reply with Quote
Thanks everyone. i've implemented all the changes mentioned.but
performance wise no variance.Thanks anyway

quote:
Originally posted by nevzab

Is this any better?

select

CASE WHEN isnull(RACCT,'') = '' THEN 'No_Account'
ELSE 'AC_' + RACCT + '' END AS Account
From DBO.GL


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/19/2013 :  01:01:40  Show Profile  Reply with Quote
quote:
Originally posted by nevzab

Is this any better?

select

CASE WHEN isnull(RACCT,'') = '' THEN 'No_Account'
ELSE 'AC_' + RACCT + '' END AS Account
From DBO.GL



still the same other than a small overhead may be due to use of function

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

India
370 Posts

Posted - 06/19/2013 :  01:15:36  Show Profile  Reply with Quote
hi visakh,
I need one more help.
can you have a look on the topic "query tuning"

quote:
Originally posted by visakh16

quote:
Originally posted by nevzab

Is this any better?

select

CASE WHEN isnull(RACCT,'') = '' THEN 'No_Account'
ELSE 'AC_' + RACCT + '' END AS Account
From DBO.GL



still the same other than a small overhead may be due to use of function

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000