< Blog

Extracting the User Name from a Computer

By Garth Jones

This question was asked in the Microsoft TechEd forum, “How do I get the user name from the PC name when the PC name has two different formats (XX-XX-XXX-XXXXXXX-X and XX-XX-XXX-XXXXXXX)?”

In this particular case an extra character was added to certain PCs to indicate that they were rebuilt in order to avoid duplicate PC names in AD.

Given that in this example the UserID is always 7 characters long you can use this substring command SUBSTRING(XX-XX-XXX-XXXXXXX,11,7).

This is what the query would look like:

Select

       Name0,

       SUBSTRING(Name0,11,7)

from

       v_r_system R

By using this query it will provide you with the user name from the computer name.

For another day, I will explain why PC names should never be based on an office location or a UserID.

Extracting the User Name from the Computer