Results 1 to 8 of 8

Thread: SELECT Some words in a field!Please Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    14

    Question SELECT Some words in a field!Please Help

    Hi All,

    can anyone help me to select the first five words in a field of a table?
    Because i want these five words so that i can make it as a link.

    any hand will be very apreciated!.

    Minh Anh.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: SELECT Some words in a field!Please Help

    What if the field has just one word? What front end language are you using? I'd suggest you use a front end language to parse the entire string you receive, since front-end languages usually have better string manipulation functions than (T?)SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    14

    Re: SELECT Some words in a field!Please Help

    I use ASP.NET,and i want the words i get will be the link in the datagrid.Please help me

    Minh Anh.

  4. #4
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: SELECT Some words in a field!Please Help

    I dont know the code but Im thinkng you could do someting along the lines of-

    Store the text to a variable

    Set a count = 0
    Set 5 variables one for each word
    Use a Loop and the Right function to move accross the text by one while and add to the count one each time while the value is not ="". Whilst looping through the characters build up the word in the first variable. On breaking the loop you will have stored in the count the current position. from there you will know the start postition of the second word. You could then peapat this for all five words by adding a second loop around the first which counts to five. An array may make it easier.
    Someone else will know a better way but just thought id have a stab at it.

    Ps this assumes you use codebehind such as vb.net although I think it can be done this way in T-SQL Also
    Last edited by FishGuy; May 6th, 2005 at 06:01 AM. Reason: added detail

  5. #5
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: SELECT Some words in a field!Please Help

    This works if you can get your text into a variable.
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim TheSentence As String
    3.         TheSentence = "The red rabbit ran round the ragged rock2"
    4.         Dim Word1 As String
    5.         Dim Word2 As String
    6.         Dim Word3 As String
    7.        
    8.  
    9.         Dim strLen As Integer = Len(TheSentence)
    10.         TheSentence.IndexOf(" ")
    11.         Word1 = Left(TheSentence, TheSentence.IndexOf(" "))
    12.         Label1.Text = Word1
    13.         TheSentence = TheSentence.Remove(0, TheSentence.IndexOf(" "))
    14.         TheSentence = LTrim(TheSentence)
    15.         Word2 = Left(TheSentence, TheSentence.IndexOf(" "))
    16.         Label2.Text = Word2
    17.         TheSentence = TheSentence.Remove(0, TheSentence.IndexOf(" "))
    18.         TheSentence = LTrim(TheSentence)
    19.         Word3 = Left(TheSentence, TheSentence.IndexOf(" "))
    20.         Label3.Text = Word3
    21.         TheSentence = TheSentence.Remove(0, TheSentence.IndexOf(" "))
    22.         TheSentence = LTrim(TheSentence)
    23.         Label4.Text = Word1 & Word2 & Word3
    24.  
    25.  
    26.  
    27.     End Sub
    You could even use a loop to replace repeating someof the code

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: SELECT Some words in a field!Please Help

    How about:
    VB Code:
    1. Dim TheSentence As String = "The red rabbit ran round the ragged rock2"
    2.  
    3.         Dim sWords() As String = System.Text.RegularExpressions.Regex.Split(TheSentence, " ")
    4.         For i As int16 = 0 to 4
    5.             Label1.Text &= sWord(i)
    6.         Next
    Last edited by wild_bill; May 6th, 2005 at 04:37 PM.

  7. #7
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: SELECT Some words in a field!Please Help

    Some sort of condition would need to be added to just get the first 5 words.

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    14

    Re: SELECT Some words in a field!Please Help?Solved

    Hi Mendhak,Wild Bill,FishGuy!.

    My Problem has been solved.thank you very for your help!.
    Minh Anh.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width