简体   繁体   中英

In SQL Server, how do you replace part of a string value with a blank value and print the columns in correct order?

I am writing a script using SQL Server code but it's not in SQL Server it is another data utility tool and it does not provide any feedback on errors or why I'm getting them.

I have a particular column that involves emails. A value from that column for example may be "sam123@docoschools.org". I am trying to only return the "sam123" for that column. To do this, I tried using this code:

Replace(c.email, '@docoschools.org', ' ') as Email

but it still comes back as "sam123@docoschools.org". What am I doing wrong here? And it's not printing in the order I am selecting the columns. If I select the email column before the replace clause and use join contact c on c.personid = p.personid after join [Identity] i with(nolock) on i.identityID = p.currentIdentityID , then it will run.

But if I try to select the email column after the replace statement, and keep the join in the same place it won't run. I am trying to figure out where to add the join and what join to add to make the email column come last. I tried left join contact c on c.personid = p.personid after left join [Identity] it on it.identityID = tp.currentIdentityID and it doesn't run:


select distinct 
i.lastname as LINC_DBTSIS_CE020_LST_NME,
i.firstname as FRST_NME,
sl.number as SCH_NMR,
it.lastName  as LINC_DBTSIS_SY030_LST_NME,
tp.staffnumber as TCHR_NBR,
p.studentnumber as ID_NBR,
e.grade as GRDE,

replace(replace(replace(l.householdPhone,'(',''),')',''),'-','') as F1_PHNE
    from Person p with(nolock)
    join [Identity] i with(nolock) on i.identityID = p.currentIdentityID

    INNER JOIN enrollment e with(nolock) ON e.personID = p.personID AND e.enrollmentID =
    (SELECT TOP 1 x.enrollmentID FROM enrollment x
        INNER JOIN schoolyear syx with(nolock) ON syx.endyear = x.endyear AND syx.active = 1
        WHERE x.personID = p.personID AND x.endyear = e.endyear and x.active = 1
        ORDER BY CASE WHEN x.enddate IS NULL THEN 0 ELSE 1 END,CASE WHEN x.serviceType = 'P' THEN 1 ELSE 2 END, x.startDate DESC)
replace(c.email, '@docoschools.org', ' ') as Email
Join calendar cl with(nolock) on cl.calendarID = e.calendarID 
join school sl on sl.schoolID = cl.schoolID 
left join v_CensusContactSummary l on l.personID = p.personid 
left join person tp on tp.personID = dbo.fn_gethr_personID(e.enrollmentID)
left join [Identity] it on it.identityID = tp.currentIdentityID
left join contact c on c.personid = p.personid
where l.relationship = 'self'

You would need to rearrange some stuff. I also added a little bit of formatting so this is a lot easier to decipher.

select distinct 
    i.lastname as LINC_DBTSIS_CE020_LST_NME,
    i.firstname as FRST_NME,
    sl.number as SCH_NMR,
    it.lastName  as LINC_DBTSIS_SY030_LST_NME,
    tp.staffnumber as TCHR_NBR,
    p.studentnumber as ID_NBR,
    e.grade as GRDE,
    replace(replace(replace(l.householdPhone,'(',''),')',''),'-','') as F1_PHNE,
    replace(c.email, '@docoschools.org', ' ') as Email
from Person p
join [Identity] i on i.identityID = p.currentIdentityID
INNER JOIN enrollment e ON e.personID = p.personID 
                        AND e.enrollmentID = (  SELECT TOP 1 x.enrollmentID 
                                                FROM enrollment x
                                                INNER JOIN schoolyear syx ON syx.endyear = x.endyear 
                                                    AND syx.active = 1
                                                WHERE x.personID = p.personID 
                                                    AND x.endyear = e.endyear 
                                                    and x.active = 1
                                                ORDER BY CASE WHEN x.enddate IS NULL THEN 0 ELSE 1 END
                                                    , CASE WHEN x.serviceType = 'P' THEN 1 ELSE 2 END
                                                    , x.startDate DESC
                                                )
Join calendar cl on cl.calendarID = e.calendarID 
join school sl on sl.schoolID = cl.schoolID 
left join v_CensusContactSummary l on l.personID = p.personid 
left join person tp on tp.personID = dbo.fn_gethr_personID(e.enrollmentID)
left join [Identity] it on it.identityID = tp.currentIdentityID
left join contact c on c.personid = p.personid
where l.relationship = 'self'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM