Solved

Query help

Posted on 2007-04-04
8
234 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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
 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

825 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