Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SYSTEM_USER removing characters

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-06-16 : 15:12:52
I use the SYSTEM_USER built-in function to capture who edited a record in several of my SQL tables. However, the domain name is put at the beginning of the username, such as FNC/Brittney for example. I need to trim off those first 4 letters, so it just shows Brittney. Should I do that with a replace after it's been inserted, or what would be the best approach? Currently a trigger puts the username in there:


ALTER TRIGGER [dbo].[TRIGGERNAME] ON [dbo].[TableA]
FOR UPDATE AS
begin
update TableA
set Modified_By = SYSTEM_USER
FROM TableA A join INSERTED i on A.id= i.id
end

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-06-16 : 15:23:43
might be better to do it at insertion. if you do it after the fact you might get into a cyclical thing where now your trigger runs again and again.

I would use SUBSTRING with PATINDEX and all


If you don't have the passion to help people, you have no passion
Go to Top of Page

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-06-16 : 15:29:02
I'm wondering why this doesn't work.



ALTER TRIGGER [dbo].[TRIGGERNAME] ON [dbo].[TableA]
FOR UPDATE AS
begin
update TableA
set Modified_By = replace(SYSTEM_USER, 'FNC/', '')
FROM TableA A join INSERTED i on A.id= i.id
end

Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-06-16 : 16:33:46
should work depends what it is that you are doing in that join

If you don't have the passion to help people, you have no passion
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-06-16 : 17:20:30
ALTER TRIGGER [dbo].[TRIGGERNAME] ON [dbo].[TableA]
FOR UPDATE AS
begin
update TableA
set Modified_By = replace(SYSTEM_USER, 'FNC\', '')
FROM TableA A join INSERTED i on A.id= i.id
end

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -