Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2007
    Posts
    12

    Unanswered: parse data and then compare

    I am using MSSQL v8 (if that matters)

    The data looks like the following
    ---------------------------
    | PBP 20070420 2:26pm |
    ---------------------------

    Now the data in this field is not uniform it can be blank, a sentence or have a different pre fix, instead of PBP, but the date will be YYYYMMDD when it is supplied.

    I need to find all the dates that are within the last 10 months. How do I perform this task?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    yes, it matters -- moving thread to SQL Server forum

    will the date always always follow the first space?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    you're going to have to supply more than 1 sample row
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  4. #4
    Join Date
    Jun 2007
    Posts
    12
    Sorry about the inactivity, i have been busy with reviews.

    The date will always be after the first spaces if the date is supplied.

    Now I did not write the database or the program, just generating reports from it, so i am not responsible for the disorganization. The filed was originally a list of cities, so there are still some city names in this field, along with blank data, and the data we need.

    The data we need is formated as [<username> <YYYYMMDD> <time>]
    here are some examples
    Code:
    ---------------------------
    | MCG     20050624 1:28pm |
    ---------------------------
    | PBP     2005072711:38am |
    ---------------------------
    |                         |
    ---------------------------
    | Atlanta                 |
    ---------------------------
    | JM      20050721 4:48pm |
    ---------------------------
    This shows the 3 types of data that i encounter in this table.
    Last edited by problemss; 08-16-07 at 17:54.

  5. #5
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Quote Originally Posted by problemss
    The date will always be after the first spaces if the date is supplied.
    Your sample data suggest more than one space. Is the <username> a fixed length then use substring to get the date part else
    use patindex to find the starting position of the date.
    Code:
    select c1=
    'MCG     20050624 1:28pm'  into #t1 union all select
    'PBP     20050727 11:38am' union all select
    ''                         union all select
    'Atlanta'                  union all select
    'JM      20050721 4:48pm'  union all select
    'PBP 20070420 2:26pm'
    
    select c1 from #t1
    where patindex('% [12][0-9][0-9][0-9][01][0-9][0-3][0-9]%',c1)>0
      and dateadd(mm,-10,getdate())<(substring(c1,patindex('% [12][0-9][0-9][0-9][01][0-9][0-3][0-9]%',c1)+1,26))
    
    drop table #t1
    Last edited by pdreyer; 08-17-07 at 04:12.

Posting Permissions

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