• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Query help

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
jcollazo81
Asked:
jcollazo81
  • 4
  • 3
1 Solution
 
DocGyverCommented:
Well you can use

CAST(STRIKE * 1000 AS INT)

to get your price normalized to the number of digits you want
0
 
jcollazo81Author Commented:
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
 
DocGyverCommented:
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
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!

 
ksaulCommented:
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
 
DocGyverCommented:
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
 
jcollazo81Author Commented:
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
 
DocGyverCommented:
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
 
jcollazo81Author Commented:
Perfect the thing about this is our data for that strike doesn't go over 5 char. Thank you so much!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now