Results 1 to 10 of 10

Thread: How to manipulate a string field in a SQL statement ?

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Resolved How to manipulate a string field in a SQL statement ?

    I am grabbing a field from a table that is defined as a string. It is actually a date in the format YYYYMMDD.
    What I would like to do is put something in the SQL statement so that when it comes back it reads as "DD/MM/YYYY".

    Is this easy enuff to do ?
    Last edited by TheBionicOrange; Jul 20th, 2006 at 04:18 AM.

  2. #2

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: How to manipulate a string field in a SQL statement ?

    OK found it.

    I used :

    VB Code:
    1. SELECT     SUBSTRING(Audit_Date, 7, 2) + '/' + SUBSTRING(Audit_Date, 5, 2) + '/' + SUBSTRING(Audit_Date, 1, 4) AS Expr1
    2. FROM         Audit_Location_Summary


  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: How to manipulate a string field in a SQL statement ?

    You could just format the date field value once you get it:

    MyStr = Format(MyRS.Fields("Audit_Date").Value,"dd/mm/yyyy")
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: How to manipulate a string field in a SQL statement ?

    No I couldn't, well not in this case. The recordset is being thrown straight at a grid. Thats why I needed to get it sorted within the SQl.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: How to manipulate a string field in a SQL statement ?

    Then format the grid column.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to manipulate a string field in a SQL statement ?

    As long as you are using a "proper" database (like SQL Server, as I think TheBionicOrange is) it's much more efficient to do it in the SQL.

  7. #7
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Re: How to manipulate a string field in a SQL statement ?

    if you are using SQL Server then its a lot simpler. You can just construct your sql statement to convert your string into a date


    Code:
    select convert(varchar(10), convert(datetime, YourStringDateField), 103) from YourTable.....
    the general syntax is
    convert(datatype, FieldToConvert, FormatOptions)

    In our case formatOption 103 will give you string in a date-like format dd/mm/yyyy

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to manipulate a string field in a SQL statement ?

    I've just seen a ghost!!

    Welcome back Serge!


    You are indeed correct, I should have thought of that.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to manipulate a string field in a SQL statement ?

    Quote Originally Posted by si_the_geek
    I've just seen a ghost!!

    Welcome back Serge!
    A ghost for sore eyes!

    I had to double check the date time stamp on the post. I thought for a moment someone had posted in the very old thread. Nice to see a Serge post again. Hopefully, the time between this one and the last one won't be as long.

  10. #10

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: How to manipulate a string field in a SQL statement ?

    Thanks for that Serge .. I'll make a note of that one.

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