Solved

Query help

Posted on 2007-04-04
8
214 Views
Last Modified: 2013-11-16
SELECT case positiontype when '1' then 'B' when '-1' then 'S' else 'none' end as [B/S], CAST(TradePrice as float(20,8) )AS [PRICE], Quantity AS VOLUME,
COMMENT,
'LO'  + substring(Contract, 2,1) + ' ' + RIGHT(Contract, 2) +SUBSTRING(cast( expiringdate as varchar(10)),5,2) + LEFT(Contracttype, 1) as FEEDCODE, 'Liffe Futures' as PORTFOLIO, 'ImportTrades' as MARKET, CONTRACTTYPE
FROM TS_Positions
WHERE     ( Contract LIKE 'LI%'  AND  PositionType = 1 )
OR            (  Contract LIKE 'LK%' AND PositionType = - 1 )
OR            ( Contract LIKE 'LL%')
OR            (Contract LIKE 'LM%') order by FEEDCODE

I would like to add this to the FEEDCODE column after the the substring-expiring date-data

Column is called STRIKE in DATABASE also i went to Desgin table and that column strike is float is the DATATYPE  8 is the Length

All information about the strike is needed, e.g.:
DAtabase current data=> would like to see this change in Feedcode column
97.5(decimal)=> the five digits after the date are 97500
97    => the five digits after the date are 97000
94.125 => the five digits after the date are 94125
129.5 => the five digits after the date are 12950
129 => the five digits after the date are 12900

Current query result:
b/s            price                 volume          comment       feedcode       portfolio        market          contractype
B      0.0      2000      NULL      LOI 0601C      Liffe Futures      ImportTrades      Call

would like to see query result:
b/s            price                 volume          comment       feedcode       portfolio        market          contractype
B      0.0      2000      NULL      LOI 060197500C      Liffe Futures      ImportTrades      Call

b/s            price                 volume          comment       feedcode       portfolio        market          contractype
B      0.0      2000      NULL      LOI 060197000C      Liffe Futures      ImportTrades      Call


b/s            price                 volume          comment       feedcode       portfolio        market          contractype
B      0.0      2000      NULL      LOI 060194125C      Liffe Futures      ImportTrades      Call

Notice all end with 5 char
How would you suggest i write this query?  
0
Comment
Question by:jcollazo81
  • 4
  • 3
8 Comments
 
LVL 6

Expert Comment

by:DocGyver
ID: 18852064
Well you can use

CAST(STRIKE * 1000 AS INT)

to get your price normalized to the number of digits you want
0
 

Author Comment

by:jcollazo81
ID: 18852088
I added that on there but it didn't like that:

Server: Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value 'LOK 0704' to a column of data type int.


SELECT case positiontype when '1' then 'B' when '-1' then 'S' else 'none' end as [B/S], CAST(TradePrice as float(20,8) )AS [PRICE], Quantity AS VOLUME,
COMMENT,
'LO'  + substring(Contract, 2,1) + ' ' + RIGHT(Contract, 2) +SUBSTRING(cast( expiringdate as varchar(10)),5,2) + CAST(STRIKE * 1000 AS INT)+ LEFT(Contracttype, 1) as FEEDCODE, 'Liffe Futures' as PORTFOLIO, 'ImportTrades' as MARKET, CONTRACTTYPE as KIND
FROM TS_Positions
WHERE     ( Contract LIKE 'LI%'  AND  PositionType = 1 )
OR            (  Contract LIKE 'LK%' AND PositionType = - 1 )
OR            ( Contract LIKE 'LL%')
OR            (Contract LIKE 'LM%') order by FEEDCODE

0
 
LVL 6

Expert Comment

by:DocGyver
ID: 18852093
So if I take your query and add in the STRIKE column to the FEEDCODE result you would have

SELECT case positiontype when '1' then 'B' when '-1' then 'S' else 'none' end as [B/S], CAST(TradePrice as float(20,8) )AS [PRICE], Quantity AS VOLUME,
COMMENT,

'LO'  + substring(Contract, 2,1) + ' ' + RIGHT(Contract, 2) +SUBSTRING(cast( expiringdate as varchar(10)),5,2) + LEFT(Contracttype, 1) +' ' +cast(cast(STRIKE *1000 as int) as varchar) as FEEDCODE,

'Liffe Futures' as PORTFOLIO, 'ImportTrades' as MARKET, CONTRACTTYPE
FROM TS_Positions
WHERE     ( Contract LIKE 'LI%'  AND  PositionType = 1 )
OR            (  Contract LIKE 'LK%' AND PositionType = - 1 )
OR            ( Contract LIKE 'LL%')
OR            (Contract LIKE 'LM%') order by FEEDCODE
0
 
LVL 10

Expert Comment

by:ksaul
ID: 18852097
SELECT case positiontype when '1' then 'B' when '-1' then 'S' else 'none' end as [B/S], CAST(TradePrice as float(20,8) )AS [PRICE], Quantity AS VOLUME,
COMMENT,
'LO'  + substring(Contract, 2,1) + ' ' + RIGHT(Contract, 2) +SUBSTRING(cast( expiringdate as varchar(10)),5,2) + LEFT(Contracttype, 1)
+ LEFT(REPLACE(convert(varchar(6),STRIKE),'.','') + '00000', 5) + '     ' as FEEDCODE, 'Liffe Futures' as PORTFOLIO, 'ImportTrades' as MARKET, CONTRACTTYPE
FROM TS_Positions
WHERE     ( Contract LIKE 'LI%'  AND  PositionType = 1 )
OR            (  Contract LIKE 'LK%' AND PositionType = - 1 )
OR            ( Contract LIKE 'LL%')
OR            (Contract LIKE 'LM%') order by FEEDCODE
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Expert Comment

by:DocGyver
ID: 18852107
sorry I put it in the wrong place.  Didn't notice that you wanted it right after the date.  also I included a space delimeter so the final result would be:

SELECT case positiontype when '1' then 'B' when '-1' then 'S' else 'none' end as [B/S], CAST(TradePrice as float(20,8) )AS [PRICE], Quantity AS VOLUME,
COMMENT,
'LO'  + substring(Contract, 2,1) + ' ' + RIGHT(Contract, 2) +SUBSTRING(cast( expiringdate as varchar(10)),5,2) + CAST(CAST(STRIKE * 1000 AS INT) AS VARCHAR)+ LEFT(Contracttype, 1) as FEEDCODE, 'Liffe Futures' as PORTFOLIO, 'ImportTrades' as MARKET, CONTRACTTYPE as KIND
FROM TS_Positions
WHERE     ( Contract LIKE 'LI%'  AND  PositionType = 1 )
OR            (  Contract LIKE 'LK%' AND PositionType = - 1 )
OR            ( Contract LIKE 'LL%')
OR            (Contract LIKE 'LM%') order by FEEDCODE
0
 

Author Comment

by:jcollazo81
ID: 18852140
Yeah it worked! Thanks! Quick question.
some results that just have 0 in it just shows for future(future will always show 0):
LOI 06090F
is there anyway i can show 0000(adding the 0000 if result =0?)
LOI 060900000F

here one good result for 97375:
LOI 060197375P

MAkes sense?



SELECT case positiontype when '1' then 'B' when '-1' then 'S' else 'none' end as [B/S], CAST(TradePrice as float(20,8) )AS [PRICE], Quantity AS VOLUME,
COMMENT,
'LO'  + substring(Contract, 2,1) + ' ' + RIGHT(Contract, 2) +SUBSTRING(cast( expiringdate as varchar(10)),5,2) +  cast(cast(STRIKE *1000 as int) as varchar)+ LEFT(Contracttype, 1) as FEEDCODE, 'Liffe Futures' as PORTFOLIO, 'ImportTrades' as MARKET, CONTRACTTYPE as KIND
FROM TS_Positions
WHERE     ( Contract LIKE 'LI%'  AND  PositionType = 1 )
OR            (  Contract LIKE 'LK%' AND PositionType = - 1 )
OR            ( Contract LIKE 'LL%')
OR            (Contract LIKE 'LM%') order by FEEDCODE
0
 
LVL 6

Accepted Solution

by:
DocGyver earned 500 total points
ID: 18853048
Couple of possibilities to consider there.

The first is what you want to do when the number of characters might be more than 5.  For instance if you have the number: 102.309 then you would end up with a result of 102309 which doesn't fit in 5 characters.

The second is for results that will be less than 5 characters but still not zero (0).  For instance the number: 1.03 would end up looking like just plain 1030.

Let's say you want to always be 5 characters.  To do that you can use

RIGHT('00000'+ cast(cast(STRIKE *1000 as int) as varchar) , 5)

resulting in the following:

0  ->           00000
1.03 ->       01030
102.309 -> 02309  ****you will lose an important digit with this!

If you use LEFT() to get the left most characters then you will not be able to tell where the implied decimal place is but to show the results you would get
0  ->           00000
1.03 ->       10300  *
102.309 -> 10230  *+
+ you will lose the ending digit
* your implied decimal is different.

So finally for a result that keeps your implied decimal correct and gives you all your information.  The sacrifice being it could result in more than 5 characters.

ISNULL(REPLICATE('0', 5-LEN( cast(cast(STRIKE *1000 as int) as varchar)  )), '') +  cast(cast(STRIKE *1000 as int) as varchar)

basically all that does it give you 5 minus the length of the result '0' characters at the front.  If your result is already 5 or longer characters you get no leading 0 characters.
This will result in:
0  ->             00000
1.03 ->         01030
102.309 -> 102309
0
 

Author Comment

by:jcollazo81
ID: 18853176
Perfect the thing about this is our data for that strike doesn't go over 5 char. Thank you so much!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now