Unix Technical Forum

PROBLEM using CharIndex(), IF, OR etc- PLEASE HELP

This is a discussion on PROBLEM using CharIndex(), IF, OR etc- PLEASE HELP within the SQL Server forums, part of the Microsoft SQL Server category; --> What is the best way to essentially use the charindex(find) function if the value is could be more than ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:03 PM
matthewwhaley@gmail.com
 
Posts: n/a
Default PROBLEM using CharIndex(), IF, OR etc- PLEASE HELP

What is the best way to essentially use the charindex(find) function
if the value is could be more than one variable (A or B or C)

I can't seem to get an "or", "if" or "select if" to work


Below is the code that works, I need 'GOV' to be either 'GOV' or 'FWD'
or 'LIB'

sqlstring = "SELECT Distinct substring([exposurename]," _
& Len(Worksheets(4).Range("j5") & "_VAR_" _
&
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 2 _
& ",charindex('GOV',[exposurename])-" &
Len(Worksheets(4).Range("j5") _
& "_VAR_" &
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
& ") AS Drivergrp2 " _
& "FROM mars.dbo.mroInventoryProductGreeks_Latest
" _
& "Where producttype = 'creditdefaultswap' " _
& "AND exposureName like '" &
Worksheets(4).Range("j5") & "_VAR_" &
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0) & "%' "
_

Much appreciation if you can help

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:03 PM
SB
 
Posts: n/a
Default Re: PROBLEM using CharIndex(), IF, OR etc- PLEASE HELP

On May 11, 5:09 am, matthewwha...@gmail.com wrote:
> What is the best way to essentially use the charindex(find) function
> if the value is could be more than one variable (A or B or C)
>
> I can't seem to get an "or", "if" or "select if" to work
>
> Below is the code that works, I need 'GOV' to be either 'GOV' or 'FWD'
> or 'LIB'
>
> sqlstring = "SELECT Distinct substring([exposurename]," _
> & Len(Worksheets(4).Range("j5") & "_VAR_" _
> &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 2 _
> & ",charindex('GOV',[exposurename])-" &
> Len(Worksheets(4).Range("j5") _
> & "_VAR_" &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
> & ") AS Drivergrp2 " _
> & "FROM mars.dbo.mroInventoryProductGreeks_Latest
> " _
> & "Where producttype = 'creditdefaultswap' " _
> & "AND exposureName like '" &
> Worksheets(4).Range("j5") & "_VAR_" &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0) & "%' "
> _
>
> Much appreciation if you can help


Hi,
See if this works. You may have to play with double quotes to get it
working since I do not know VB or excel programming.

sqlstring = "SELECT Distinct substring([exposurename]," _
& Len(Worksheets(4).Range("j5") & "_VAR_" _
&
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 2 _
& ",
isnull(nullif(isnull(nullif(charindex('GOV',[exposurename])-" &
Len(Worksheets(4).Range("j5") _
& "_VAR_" &
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
& ",0), charindex('FWD',[exposurename])-" &
Len(Worksheets(4).Range("j5") _
& "_VAR_" &
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
& "),0), charindex('LIB',[exposurename])-"
&
Len(Worksheets(4).Range("j5") _
& "_VAR_" &
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
& ")
) AS Drivergrp2 " _
& "FROM mars.dbo.mroInventoryProductGreeks_Latest
" _
& "Where producttype = 'creditdefaultswap' " _
& "AND exposureName like '" &
Worksheets(4).Range("j5") & "_VAR_" &
Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0) & "%' "

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:03 PM
SB
 
Posts: n/a
Default Re: PROBLEM using CharIndex(), IF, OR etc- PLEASE HELP

On May 11, 11:02 am, SB <othell...@yahoo.com> wrote:
> On May 11, 5:09 am, matthewwha...@gmail.com wrote:
>
>
>
>
>
> > What is the best way to essentially use the charindex(find) function
> > if the value is could be more than one variable (A or B or C)

>
> > I can't seem to get an "or", "if" or "select if" to work

>
> > Below is the code that works, I need 'GOV' to be either 'GOV' or 'FWD'
> > or 'LIB'

>
> > sqlstring = "SELECT Distinct substring([exposurename]," _
> > & Len(Worksheets(4).Range("j5") & "_VAR_" _
> > &
> > Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 2 _
> > & ",charindex('GOV',[exposurename])-" &
> > Len(Worksheets(4).Range("j5") _
> > & "_VAR_" &
> > Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
> > & ") AS Drivergrp2 " _
> > & "FROM mars.dbo.mroInventoryProductGreeks_Latest
> > " _
> > & "Where producttype = 'creditdefaultswap' " _
> > & "AND exposureName like '" &
> > Worksheets(4).Range("j5") & "_VAR_" &
> > Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0) & "%' "
> > _

>
> > Much appreciation if you can help

>
> Hi,
> See if this works. You may have to play with double quotes to get it
> working since I do not know VB or excel programming.
>
> sqlstring = "SELECT Distinct substring([exposurename]," _
> & Len(Worksheets(4).Range("j5") & "_VAR_" _
> &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 2 _
> & ",
> isnull(nullif(isnull(nullif(charindex('GOV',[exposurename])-" &
> Len(Worksheets(4).Range("j5") _
> & "_VAR_" &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
> & ",0), charindex('FWD',[exposurename])-" &
> Len(Worksheets(4).Range("j5") _
> & "_VAR_" &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
> & "),0), charindex('LIB',[exposurename])-"
> &
> Len(Worksheets(4).Range("j5") _
> & "_VAR_" &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0)) + 3 _
> & ")
> ) AS Drivergrp2 " _
> & "FROM mars.dbo.mroInventoryProductGreeks_Latest
> " _
> & "Where producttype = 'creditdefaultswap' " _
> & "AND exposureName like '" &
> Worksheets(4).Range("j5") & "_VAR_" &
> Worksheets(4).Range("C4").Offset(Worksheets(4).Ran ge("c3"), 0) & "%' "- Hide quoted text -
>
> - Show quoted text -


There is an elegant way to do this. However your excel ranges are too
big to accommodate that solution. Basically, you switch your string
positions in charindex function. Therefore, instead of looking for GOV
in target string, you take the whole target string and match with GOV.
It is something like:
Strip out gov etc: Substring(target_string,...)
Then you do: charindex('stripped string', 'GOV,FWD,LIB')
And if your target string is small (in your case probably it isn't)
then you can directly use it in charindex as: charindex('source
string', 'GOV,FWD,LIB')
Maybe in your excel you can create an extra column where you store the
stripped column then you can just match it with 'GOV,FWD,LIB'.
HTH.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:32 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com