| Author |
Topic |
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-03 : 12:50:05
|
| hi all!i have columns. 1. Flag : stores only + / - / <NULL>2. Number : stores floathow would i select this 2 columns to generate -ve and +ve number depend on the flag? eg : Flag : -Number : 4 ====> -4 and +4 if flag=+when i try to just to combine with -(Flag) for -ve, it gave converting type error even i already CAST it..tq in advance~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
|
|
sshelper
Posting Yak Master
216 Posts |
Posted - 2007-07-03 : 12:52:28
|
| One option is to use a CASE statement to generate the output:SELECT CASE WHEN Flag = '-' THEN -1 ELSE 1 END * YourNumber FROM YourTableSQL Server Helperhttp://www.sql-server-helper.com |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-03 : 12:53:21
|
| Why have you chosen this design? Float can easily store negative values.Store complete value in Number and use Flag as an reminder of what sign Number has, with the SIGN function.Yikes.Peter LarssonHelsingborg, Sweden |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-03 : 13:00:24
|
| THAT"S Why! unfortunately this tbl has to be leave intact. so how? temp table?~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-03 : 13:08:13
|
| What result should you get when Flag is NULL?Peter LarssonHelsingborg, Sweden |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-07-03 : 13:09:29
|
| select isnull(CAST(flag as varchar(1)), '+') + CAST(YourNumber as varchar(10)) as formatedFROM YourTable_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
RocketScientist
Official SQLTeam Chef
85 Posts |
Posted - 2007-07-03 : 13:12:59
|
| select case when flag is null then 1 when flag = '+' then 1 else -1 end * numberfrom <table>that assumes you want NULL and + to be treated the same. You'll want to mess with the defaults (yeah, it's suposed to be + - or null, but what if it isn't).A better way to store the data is to just sign the number. A second best would be to store -1 or 1 in the sign field that way it's a simple multiplication problem. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-07-03 : 18:42:58
|
whoa. i guess you can be glad there's not a separate column for each digit of the number... elsasoft.org |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-03 : 20:46:39
|
| hi spirit..thanksso i CAST to varchar first and then CAST to FLOAT, right?rocket & sshelper ----> yeaa.. i did the same thing before that gave me converting problem in my crystal report...thanks all~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-03 : 21:20:11
|
| okey spirit.. CAST in CAST = go casting... :Dwell.. this one works finallyselect CAST(isnull(CAST(adjFlag as varchar(1)), '+') + CAST(adjQty as varchar(10)) as float) as formatedfrom tblstkadjdetailtq~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
|