Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Conditional Query Problem
Old 08-26-2008, 04:39 PM Conditional Query Problem
Skilled Talker

Posts: 60
Name: Nick Martin
Trades: 0
Hey...

I am writing a rather complex query that uses several conditional satements. Below is what seems to be my trouble area. These conditions are in the select. If I just select these as NULL, the query is much faster. I thought the problem was with the first line (the convert). Comment it out had very little effect. Is there another way to perform these conditions that won't make my query so inefficient?

I appreciate any help I can get.

Thanks!



Code:
(if p.fldType = 1 then right('0000' + convert(char(4), so.fldSignYear), 4) || '-' || right('00' + convert(char(2), so.fldSignMonth), 2) || '-' || right('00' + convert(char(2), so.fldSignDay), 2) else NULL endif) as fldSOSignDate,
                (if p.fldType = 1 then so.fldInvoiceNo else NULL endif) as fldInvoiceNo,
                (if p.fldType = 1 then so.fldVehicleType else NULL endif) as fldSOVehicleType,
                (if p.fldType = 1 then so.fldTechID else NULL endif) as fldTechID,
                (if p.fldType = 1 then so.fldSplitTechID else NULL endif) as fldSplitTechID,
                (if p.fldType = 1 then so.fldVIN else NULL endif) as fldSOVIN,
                    (if p.fldType = 1 then sos.fldCarPrice else NULL endif) as fldCarPayroll,
                    (if p.fldType = 1 then sos.fldTruckPrice else NULL endif) as fldTruckPayroll,
                    (if p.fldType = 1 then sov.fldVehicleType else NULL endif) as fldSOVVehicleType,
                    (if p.fldType = 1 then sov.fldVIN else NULL endif) as fldSOVVIN,
                    (if p.fldType = 1 then sos.fldsid else NULL endif)
SpudNik is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-26-2008, 05:54 PM Re: Conditional Query Problem
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
What's p.fldType? What other values will there be? Can you just select that column, along with the others, and have the caller decide whether to use them?

Or, use a stored procedure, declare a @FieldType int variable, and set it to be either 1 or NULL. Then select so.fldInvoiceNo * @FieldType, which if it's 1, will be the same value, and if it's NULL, will return NULL.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Conditional Query Problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11506 seconds with 12 queries