Re: select LEFT

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Tamas Bojcan - bojci (bojcan_at_freemail.hu)
Date: 08/21/04


Date: Sun, 22 Aug 2004 00:00:41 +0200

Hi,

Oohh, what I do see! Thanks for correcting my stupidity!!! Of course MAX
can't be used as I wrote...

"Itzik Ben-Gan" <itzik@REMOVETHIS.SolidQualityLearning.com> wrote in message
news:OvZtJA7hEHA.4092@TK2MSFTNGP10.phx.gbl...
> MAX in SQL/T-SQL is supported only as an aggregate function accepting a
> single expression as an input. MAX doesn't support multiple arguments.
>
> How you deal with the case where there is no '/' in the input depends on
the
> requirement.
>
> If you want to get all the string:
>
> LEFT(@s, CHARINDEX('/',@s+'/')-1)
>
> If you want to get an empty string:
>
> LEFT(@s, PATINDEX('%_/%',@s))
>
> If you want to get a NULL:
>
> LEFT(@s, NULLIF(CHARINDEX('/',@s), 0)-1)
>
> --
> BG, SQL Server MVP
> www.SolidQualityLearning.com
>
>
> "Tamas Bojcan - bojci" <bojcan@freemail.hu> wrote in message
> news:OovDWs2hEHA.1972@TK2MSFTNGP09.phx.gbl...
> > Hi,
> >
> > I wrote the same as you but you used CASE statement to handle that case
> when
> > '/' has zero occurance while I used the MAX.
> >
> > In details:
> > ( CHARINDEX( '/' ,> 'left/right' )-1) < 0 when there is no '/'
> character
> > in the string so the MAX gives back 0.
> >
> >
> >
> > "Roji. P. Thomas" <lazydragon@nowhere.com> wrote in message
> > news:uN7tid1hEHA.1184@TK2MSFTNGP12.phx.gbl...
> > > >>select SUBSTRING( 'left/right', 1, MAX( CHARINDEX( '/' ,
> > > 'left/right' )-1,0 ) )
> > >
> > > What does the MAX function does there?
> > > Also what happens if '/' has zero ocurances in the string?
> > >
> > > Try
> > >
> > > DECLARE @v VARCHAR(50)
> > > SET @v = 'left/right'
> > > SELECT LEFT(@v, CASE WHEN CHARINDEX('/',@v) > 0 THEN
> CHARINDEX('/',@v) -1
> > > ELSE 0 END )
> > >
> > >
> > > --
> > > Roji. P. Thomas
> > > Net Asset Management
> > > https://www.netassetmanagement.com
> > >
> > >
> > > "Tamas Bojcan - bojci" <bojcan@freemail.hu> wrote in message
> > > news:eESlIqvhEHA.1644@tk2msftngp13.phx.gbl...
> > > > Sorry, I was wrong I forgot to decrease the CHARINDEX value...
> > > >
> > > > select SUBSTRING( 'left/right', 1,CHARINDEX( '/' ,
'left/right' )-1 )
> > > >
> > > > or if you are not sure that the string contains the character...:
> > > >
> > > > select SUBSTRING( 'left/right', 1, MAX( CHARINDEX( '/' ,
> > 'left/right' )-1,
> > > > 0 ) )
> > > >
> > > > "Tamas Bojcan - bojci" <bojcan@freemail.hu> wrote in message
> > > > news:er6svjvhEHA.384@TK2MSFTNGP10.phx.gbl...
> > > > > Hi,
> > > > >
> > > > > Use SUBSTRING and CHARINDEX. For example: select SUBSTRING(
> > > 'left/right',
> > > > 1,
> > > > > CHARINDEX( '/' , 'left/right' ) )
> > > > >
> > > > >
> > > > > "Darren Woodbrey" <darrenwoodbrey@hpfairfield.com> wrote in
message
> > > > > news:elrBogvhEHA.712@TK2MSFTNGP09.phx.gbl...
> > > > > > How can I select all character to the left of a certain
character.
> > I
> > > > need
> > > > > > to select all characters to the left of a "/". LEFT will not
work
> > > > because
> > > > > > the amount of characters will be different in each case.
Thanks!
> > > > > >
> > > > > > Darren
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Relevant Pages

  • Re: Something Positive - "Otakukin"
    ... else that repeated storytelling made the character 'real' in some ... spiritual sense, just like fantasy gods are born in the Dreaming, or out ... Or that souls aren't bound by time ...
    (rec.arts.anime.misc)
  • Re: for SImage use Func??
    ... An empty string is still ... Hmm, try this: ... But you have no way to know when you've read a empty line in a lexer routine that is reading character by character. ... Niklas Holsti ...
    (comp.lang.ada)
  • Re: The two things I liked best about the Last Episode of Terminator: SCC
    ... Maybe not much money, but money. ... it's the stupidity of the writers. ... If I can come up with a solution in under 15 seconds that doesn't require a character acting entirely out of character, ...
    (rec.arts.sf.written)
  • Re: Finding the common textual denominator
    ... strip the last character off s1 and try ... At some point, you'll end up either with the empty string, ... > Regards, Bruno. ...
    (Debian-User)
  • Re: YR guilty teens
    ... "brilliance" is a huge stretch. ... It's actually BAD writing because LML takes character stupidity WAAAY ... Bad acting, up to now, isn't something anyone could pin on Y&R. ...
    (rec.arts.tv.soaps.cbs)