sql query - sql 2000

  • quick assistance, i need to write a query to show a list of names of people who have terminated for the AD administrator.

    we have lastname firstnames ie smith(lastname) george adams (first names)

    I need to separate the firstnames into two columns if they have two names.

    ie select lastname, firstnames termination date from employees where termination date >=......

    there is one space consistently between the two names.

    so far i have this

    select *

    from (

    select lastname,

    case when substring(firstname,' ') > 0 then substring(firstname,1, substring(firstname,' ')-1) else firstname end firstname_1,

    case when substring(firstname,' ') > 0 then substring(firstname, substring(firstname,' ')+1) else null end firstname_2,

    termination_date >= 2009-01-01

    from employees

    not got the substring right.

    where termination_date >= '01-may-2008'

  • Hello,

    I suspect you want to be using CharIndex to find the location of the Space and then SubString to extract the Firstnames.

    So may be something along the lines of:-

    Select

    lastname,

    case when CharIndex(' ', firstname) > 0 Then substring(firstname,1, CharIndex(' ', firstname)-1) else firstname end firstname_1,

    case when CharIndex(' ', firstname) > 0 then Right(firstname, CharIndex(' ', firstname)+1) else null end firstname_2

    From

    employees

    Where

    termination_date >= '01-may-2008'

    Regards,

    John Marsh

    www.sql.lu
    SQL Server Luxembourg User Group

  • the results i am getting on the first couple of sample results

    lastname firstname_1 firstname_2

    Davie Michael ael James

    Eyears Barry NULL

    Murrie Amelia ia Grace

    can we get rid of the first 3 on firstname_2

    and how

    almost there

  • Hey there is a slight modification in the previous code

    just apply this code

    Select

    lastname,

    case when CharIndex(' ', firstname) > 0 Then substring(firstname,1, CharIndex(' ', firstname)-1) else firstname end firstname_1,

    case when CharIndex(' ', firstname) > 0 then Right(firstname, CharIndex(' ', firstname)-1) else null end firstname_2

    From

    employees

    Where

    termination_date >= '01-may-2008'

    here the right function since counts from last you have to again decrease the count by 1

  • Hello again,

    Did you manage to get the query working Okay, or are you still having problems?

    Regards,

    John Marsh

    www.sql.lu
    SQL Server Luxembourg User Group

  • not quite

    running the last query you gave me, had to modify column names for our database

    Select

    lastname,

    case when CharIndex(' ', firstnames) > 0 Then substring(firstnames,1, CharIndex(' ', firstnames)-1) else firstnames end firstnames_1,

    case when CharIndex(' ', firstnames) > 0 then Right(firstnames, CharIndex(' ', firstnames)-1) else null end firstnames_2,

    terminationdate

    From

    employee

    Where

    terminationdate >= '2009-05-01 00:00:00.000'

    still getting the spaces and last letter from firstname attached is a sample.

    I have checked the application and they are only putting one space between.

    the column firstname is varchar 30 characters.

    look forward to further assistance and grateful for your help

  • Hello,

    You could try replacing the Right Function with the following:-

    substring(firstname, CharIndex(' ', firstname)+1, 200)

    Note that this code assumes a first name is never longer than 200 characters.

    Regards,

    John Marsh

    www.sql.lu
    SQL Server Luxembourg User Group

  • You could try this, which would eliminate the CASE. There are probably some instances that would break it, but for cases where there is never a space in the first position of first name, and there is only one space between the two names, I think it will work ok. In fact, as I'm typing now, I can think of a co-workers name that would break it. His first name is 'De Andre'. I don't know how you could get around this kind of name. Anyhow, let us know if it works for you.

    IF OBJECT_ID('TempDB..#names','u') IS NOT NULL

    DROP TABLE #names

    CREATE TABLE #names

    (

    ID INT IDENTITY(1,1),

    LAST_NAME VARCHAR(30),

    FIRST_NAME VARCHAR(30)

    )

    INSERT INTO #names

    SELECT 'Doe','John Jacob' UNION ALL

    SELECT 'Smith','Joseph M' UNION ALL

    SELECT 'Roberts','David'

    SELECT

    last_name,

    first_name1 = SUBSTRING(first_name,1,CHARINDEX(' ',CAST(first_name AS CHAR(30)),1)),

    first_name2 = ISNULL(SUBSTRING(first_name,CHARINDEX(' ',CAST(first_name AS CHAR(30)),1)+1,30),'')

    FROM #names

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

  • thanks for that, i have been out of the office the last week so have not tried this, but its probably the best way to handle this.

    i am sure the odd name will go astray, and i am not in control with how the payroll staff enter these names.

    thanks again and I will get back to you with a result.

    trout

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply