Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Current user query

Status
Not open for further replies.

ElTech

IS-IT--Management
Apr 16, 2003
22
US

I have a query that requires that the current user's name = the username in the database
when I query "select user" I get "domain\username"

how can I tell my query that the username is the last 7 characters of the user result
 
Use the right function to pick out only the last 7 characters

Select right(fieldname,7) from table1
 
ElTech,

What if usernames are less or greater than 7 characters does this need to be taken into account?


"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
If I run SELECT right(user,7) I get the correct output

User
JohnHill

The following is not working:

select transaction from order where user=right('user',7)

0 results

select transaction from order where user=JohnHill

10 results
 
You need to place the right on the column definition like in the following

Code:
select transaction from order where right(user,7)= 'JohnHill'


"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 


We made the usernames all equal 7 characters

first 4 letters of first name and first 3 of last name
Replacing last letter with numbers if duplicates will exist

Examples: John Hill username=JohnHil
If we hire another JohnHill then username=JohnHi2
 
ooops the above won't work either because JohnHill is 8 characters. Which bring me to my first question.


What if usernames are less or greater than 7 characters does this need to be taken into account?


"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
This should work
Select ltrim(right((' '+ltrim(rtrim(isnull(fieldname,' ')))),7)) user from table1

Note: donot forget to give seven spaces between two quotes

MNK
 
mnk82o,

while your method works,

Code:
Select SubString(UserName,CharIndex('\',UserName)+1,Len(UserName)) from testUser

is probably cleaner and slightly more robust.

I know this point is now moot but thought this might shed some light on any future issues.

"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top