Query to split string into two parts

 4 Replies
 0 Subscribed to this topic
 22 Subscribed to this forum
Sort:
Author
Messages
DavidBrianCampbell
Basic Member
Posts: 13
Basic Member
    I am trying to split a number into two parts and wondering if anyone has a suggested syntax? (e.g. 107511 split into 107 and 511 and then alias each with the "AS" keyword.) Thanks!

    Chris Martin posted this when I was trying two join 3 fields into one (I'm kinda doing the reverse now):

    trim(last_name) || ', ' || trim(first_name) || ' ' || case when trim(middle_init) is null then trim(middle_init) else trim(middle_init) || '.' end
    DavidBrianCampbell
    Basic Member
    Posts: 13
    Basic Member
      To be more specific, the field that I'm trying to split is EMPLOYEE.DEPARTMENT (String [5])
      So for example I want this value: "10050" split into "100" and "50" and


      SELECT EMPLOYEE.EMPLOYEE, EMPLOYEE.DEPARTMENT, rtrim(DEPARTMENT-' ') AS LAST2 ltrim(DEPARTMENT-' ') AS FIRST3 FROM EMPLOYEE

      Thanks!
      Chris Martin
      Veteran Member
      Posts: 277
      Veteran Member
        Use the substr function:

        a) substr(department,1,3) as FIRST3
        b) substr(department,4,2) as LAST2

        The LAST2 will only give you what you want if all of your departments 5 characters long.
        k-rock
        Veteran Member
        Posts: 142
        Veteran Member
          I'm not a Crystal user, but there has to be some function like LEN in excel.

          substr(department, 1, 3) as FIRST3
          substr(department, 4, LEN(department)-3) as LAST

          that should give you the remainder of the string
          John Henley
          Posts: 3353
            Brian, are you trying to do this within the SQL command itself (i.e. you're not reporting against tables but against a SQL command [or a SQL Expression in Crystal, which uses the vendor-specific SQL syntax] (and if so, what flavor: Oracle, MSSQL, etc.?), or via a Crystal Formula? The reason I ask is because the syntax is different.
            Thanks for using the LawsonGuru.com forums!
            John