Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting first alphabet in the string

Status
Not open for further replies.

jamiljarrar1

Programmer
Jan 1, 2009
4
AE
how can i make the select query to get the first alphabet in the string. for example my string look like as

"#@$%/;Please Help me"

i want to get the first alphabet 'P'. I dont want any thing before 'P'.

Advance Thanks for your help
 
Try this for starters
Code:
CREATE TABLE [Digits] (
    [DigitID] [int] NOT NULL ,
    CONSTRAINT [PK_Digits] PRIMARY KEY  CLUSTERED 
    (
        [DigitID]
    )  ON [PRIMARY] 
) ON [PRIMARY]
GO
DECLARE @position int
SET @position = 1
WHILE @position <= 1000
BEGIN
   insert into Digits(DigitID) values (@position)
   SET @position = @position + 1
END
go

declare @s varchar(500)
select @S='#@$%/;Please Help me" '

Select Charindex(Char(digitid),@s),digitid,Char(digitid)
from Digits
where (digitid between 65 and 90 or digitid between 97 and 122) and  Charindex(Char(digitid),@s)>0
order by Charindex(Char(digitid),@s)
 
or this
Code:
CREATE TABLE [Digits] (
    [DigitID] [int] NOT NULL ,
    CONSTRAINT [PK_Digits] PRIMARY KEY  CLUSTERED 
    (
        [DigitID]
    )  ON [PRIMARY] 
) ON [PRIMARY]
GO
DECLARE @position int
SET @position = 1
WHILE @position <= 1000
BEGIN
   insert into Digits(DigitID) values (@position)
   SET @position = @position + 1
END
go
declare @s varchar(500)
select @S='#@$%/;Please Help me" '

Select Substring(@s,digitid,1),Ascii(Substring(@s,digitid,1)),digitid
from Digits
where (Ascii(Substring(@s,digitid,1)) between 65 and 90 or Ascii(Substring(@s,digitid,1)) between 97 and 122)
 
thanks pwise for your kind reply.
actually i have a table for voting
messages(messageid(int),message(varchar))
data in the table is as follows

messageid message
========= =======
1 v jameel
2 vjohn
3 /vjohn
4 @Vjameel
5 %sjohn
============================
i want to fetch only that rows whos first alphabet is v ommitting anything(special characters,numeric values) before that message
hope that this will clear my question

Thanks
 
select * from tablename where substring(message, PATINDEX ('%[a-zA-Z]%',message),1) = 'v'

is that what You are looking for?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top