Improve company productivity with a Business Account.Sign Up

x
?
Solved

Query help

Posted on 2007-04-04
8
Medium Priority
?
266 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 Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
A method of moving multiple mailboxes (in bulk) to another database in an Exchange 2010/2013/2016 environment...
Viewers will learn how the fundamental information of how to create a table.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

602 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