Solved

SQL - CASE WHEN use with CAST or defining parameters of data

Posted on 2013-12-11
4
204 Views
Last Modified: 2013-12-17
I have a situation where I need to check the character of one variable and then store a variable which I'd like to store as a number (or something I can later sum up in another query of the data) but I'm having some trouble with this step.

How do I define the characteristics of DISP_QTY in this example.

CASE WHEN PRICE > 0 THEN '5' ELSE '-5' END ???VARCHAR()??? as DISP_QTY
0
Comment
Question by:ghettocounselor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:dannygonzalez09
ID: 39712266
you can probably define 2 variables with different data types and use them in a case stmt to assign the value to DISP_QTY
0
 

Accepted Solution

by:
ghettocounselor earned 0 total points
ID: 39712275
Looks like one approach is to handle this in the subsequent query.

Leaving the above as:
CASE WHEN PRICE > 0 THEN '5' ELSE '-5' END DISP_QTY

And doing the add as:
sum(CAST(DISP_QTY AS INT)) AS PT_DISP_QTY
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39712445
You may allow the RDBMS to implicitly choose a data-type:

	-- char
	DISP_QTY = CASE WHEN PRICE > 0 THEN '5' ELSE '-5' END 

	-- integer
	DISP_QTY = CASE WHEN PRICE > 0 THEN 5 ELSE -5 END 

You can explicitly cast into the data-type you want, lets say I want money
(note: in this example, quotes may be kept or eliminated):

	DISP_QTY = cast( CASE WHEN PRICE > 0 THEN '5' ELSE '-5' END as money ) 

Open in new window

0
 

Author Closing Comment

by:ghettocounselor
ID: 39723480
reason for selecting my own comment: it worked, no one else offered anything.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question