March 23, 2010 at 10:54 am
Greetings again friends,
Perhaps, this is not possible but I wanted to get your thoughts.
I have a fieldname called responseTotal (int data type).
Then I have another fieldname called isComments (bit data type).
If isComments = 0 (no comments entered), then rather than display a value of 0 for responseTotal, we would like to have a blank space as value instead.
If, however, isComments = 1 (comments was entered as value), then assign that value to responseTotal for that particular row.
Given that responseTotal is an integer data type, is there a way to manipulate it to accept a string value?
I know I have a way of not making myself clear. Please let me know if further clarification is needed.
Again, thanks to you all for all you do here to help us.
Below is the simple-minded code:
Select rq.ReportShortName, rq.reportQTitle,rd.responseTotal as 'Reporting Week', Sum(responseTotal) as YTD,
(CASE WHEN isComments <> 0 THEN rd.responseComments
WHEN isComments <> 1 THEN ''
ELSE 'No Comments'
END)
from ReportsData rd, ReportQuestions rq WHERE rq.ReportQID = rd.ReportQID AND rd.ReportGroupID=52
GROUP BY ReportShortName,reportQTitle, responseTotal, ReportQOrder,isComments,responseComments
ORDER BY reportQOrder
March 23, 2010 at 1:52 pm
You should use equal comparison rather than not equal.
CASE isComments WHEN 1 THEN rd.responseComments
WHEN 0 THEN ''
ELSE 'No Comments'
END
March 23, 2010 at 1:58 pm
Lutz is right, it's better to expressly test a value for equality. Not equals testing may cause you problems with NULLs.
In any event, the case expression evaluates to a datatype based on the values returned, rather than the values tested. In SQL 2008, you can use the SQL_Variant datatype to mix character and numeric results.
-- CASE expression evaluates to varchar based on the values returned, not tested
declare @isComments int = 1
declare @responseComments varchar(50) = 'None of your business.'
SELECTCASE WHEN @isComments = 1 THEN @responseComments
WHEN @isComments = 0 THEN ''
ELSE 'No Comments'
END
GO
-- or, use SQL_Variant to retain the datatype
declare @isComments int = 1
declare @responseComments int = 6345789
SELECTCASE WHEN @isComments = 1 THEN cast(@responseComments as sql_variant)
WHEN @isComments = 0 THEN ''
ELSE 'No Comments'
END
GO
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 24, 2010 at 10:24 am
Thanks again great people.
I can see that I have done it again!
I didn't explain myself well.
Let me attempt to represent the issue graphically.
For the purpose of what I am trying to accomplish, I have these 2 fieldnames:
responseTotal int
responseComments nvarchar(50)
isComments bit
sample data looks like this:
ResponseTotal ResponseComments isComments
1 test true
0 No Comments false
0 No Comments false
2 Testing false
Given this sample data,
If isComments is false (if isComments = 0), then display 0 or whatever the numeric value for responseTotal for that row is.
Otherwise, display the value of responseComments under responseTotal.
So, taking the sample data, we would like to see the followining:
ResponseTotal ResponseComments isComments
test test true
0 No Comments false
0 No Comments false
2 Testing false
From above example, isComments is true (isComments =1) , indicating there is a value called test under ResponseComments.
The rest of the value for ResponseTotal are numeric because isComments is either false or responseComments has a value of No Comments.
My worry is that since responseTotal is an integer datatype, it won't allow a value of string data type.
So, my question is whether you wizards can somehow manipulate it.
Sorry for not being clear.
I hope it is a bit clearer this time.
March 24, 2010 at 10:59 am
Is it something like the following that you're looking for?
Please note that I used the STR() function to convert the numeric value to to a string data type:
DECLARE @tbl TABLE
(
ResponseTotal INT,
ResponseComments VARCHAR(20),
isComments BIT
)
INSERT INTO @tbl
SELECT 1, 'test', 'true' UNION ALL
SELECT 0, 'No Comments', 'false' UNION ALL
SELECT 0, 'No Comments', 'false' UNION ALL
SELECT 2, 'Testing', 'false'
SELECT
*,
CASE isComments
WHEN 0 THEN STR(ResponseTotal)
ELSE ResponseComments
END AS RESULT
FROM @tbl
March 24, 2010 at 12:22 pm
Did you not even look at my example casting an integer as SQL_VARIANT datatype?
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 24, 2010 at 12:26 pm
Lutz,
Your legend continues to grow everyday.
This worked like a dream!
Thank you very much.
You have bailed me out twice.
I hope I don't use up all of your good graces.
Again, thanks a lot.
March 24, 2010 at 12:43 pm
The Dixie Flatline (3/24/2010)
Did you not even look at my example casting an integer as SQL_VARIANT datatype?
The main reason why I try to avoid SQL_VARIANT is the need to cast it again once you need to use that column in a join condition.
As per BOL:
In assignments from sql_variant objects to an object that has any other data type, the sql_variant value must be explicitly cast to the data type of the destination. No implicit conversions are supported when a sql_variant value is assigned to an object that has another data type.
Therefore, it became a habit to assign a "known" data type "at the source".
March 24, 2010 at 12:49 pm
The Dixie Flatline (3/24/2010)
Did you not even look at my example casting an integer as SQL_VARIANT datatype?
I did, and I liked it.
March 24, 2010 at 7:12 pm
The main reason why I try to avoid SQL_VARIANT is the need to cast it again once you need to use that column in a join condition.
Fair enough, Lutz.
Both STR() and SQL_VARIANT solve the problem of sorting data by the result column.
Thanks, Paul.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 24, 2010 at 10:42 pm
STR has some interesting properties:
PRINT STR(-2147483647)
DECLARE @tbl
TABLE (
ResponseTotal INT,
ResponseComments VARCHAR(20),
isComments BIT
);
INSERT @tbl
SELECT 1, 'test', 'true' UNION ALL
SELECT 0, 'No Comments', 'false' UNION ALL
SELECT 0, 'No Comments', 'false' UNION ALL
SELECT -2147483647, 'Testing', 'false'
SELECT
*,
CASE isComments
WHEN 0 THEN STR(ResponseTotal)
ELSE ResponseComments
END AS RESULT
FROM @tbl;
Output:
ResponseTotal ResponseComments isComments RESULT
1 test 1 test
0 No Comments 0 0
0 No Comments 0 0
-2147483647 Testing 0 **********
The data type of the RESULT column in that example is VARCHAR(20) - from the ResponseComments column - since VARCHAR has a higher data type precedence than the CHAR(10) returned by STR.
Paul
March 25, 2010 at 6:41 am
Paul, please tell me you don't have all that committed to memory. 😉
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 25, 2010 at 7:12 am
The Dixie Flatline (3/25/2010)
Paul, please tell me you don't have all that committed to memory. 😉
Ha...no, not at all. I remember very few facts, since they do not age well 🙂
Books Online :w00t:
March 25, 2010 at 7:21 am
Looking at the number of posts you've been involved in lately, and extrapolating from that a certain percentage of BOL lookups, factor in typing time.....
Tell me you have a life outside SQL.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 25, 2010 at 7:35 am
The Dixie Flatline (3/25/2010)
Tell me you have a life outside SQL.
(0 row(s) affected)
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply