| Author |
Topic |
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-14 : 04:12:32
|
| Hi,I have 2 table. T1 and T2 as belowT1userid department____________________john NULLpeter NULLT2useremail Department___________________________john@email.com ITpeter@email.com ACCount.....@email.com SaleMy question is how to i map T2 to T1 where the email is fix to userid + domain name.UPDATE T1 SET department= (SELECT department FROM t2 WHERE department = t1.department)my problem is how to subtract the useremail peter@email.com = to peter ??thanks |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-14 : 04:24:39
|
| Select left('peter@email.com',charindex('@','peter@email.com')-1)MadhivananFailing to plan is Planning to fail |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-14 : 04:42:26
|
quote: Originally posted by madhivanan Select left('peter@email.com',charindex('@','peter@email.com')-1)MadhivananFailing to plan is Planning to fail
Thanks Madhivanan,This query doesn't work.but after i take out the -1 then it will retrieve peter@ but not peter.why i can't put -1 there? |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-14 : 04:52:11
|
quote: Originally posted by madhivanan Select left('peter@email.com',charindex('@','peter@email.com')-1)MadhivananFailing to plan is Planning to fail
One more funny thing happen, when i change -1 to +1 it work and what i get is peter@e.Why we can't put -1? is there other way to do that? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 05:02:42
|
quote: Originally posted by calvinkwoo3000
quote: Originally posted by madhivanan Select left('peter@email.com',charindex('@','peter@email.com')-1)MadhivananFailing to plan is Planning to fail
One more funny thing happen, when i change -1 to +1 it work and what i get is peter@e.Why we can't put -1? is there other way to do that?
no problem in putting -1. can you show query used? also what happened when u put -1? did it gave any error? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 05:10:46
|
try this outUPDATE t1SET t1.department=t2.departmentFROM T1 t1JOIN T2 t2ON t1.userid=LEFT(t2.useremail,CASE WHEN CHARINDEX('@',t2.useremail)>0 THEN CHARINDEX('@',t2.useremail) ELSE LEN(t2.useremail)+1 END -1) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-14 : 05:53:50
|
[code]DECLARE @t1 TABLE ( userID VARCHAR(20), department VARCHAR(20) )INSERT @t1SELECT 'john', NULL UNION ALLSELECT 'peter', NULLDECLARE @t2 TABLE ( userEmail VARCHAR(100), Department VARCHAR(100) )INSERT @t2SELECT 'john@email.com', 'IT' UNION ALLSELECT 'peter@email.com', 'ACCount' UNION ALLSELECT '.....@email.com', 'Sale'SELECT *FROM @t1UPDATE xSET x.department = g.departmentFROM @t1 AS xINNER JOIN @t2 AS g ON LEFT(g.useremail, charindex('@', g.useremail) - 1) = x.useridSELECT *FROM @t1[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-14 : 05:58:52
|
quote: Originally posted by visakh16 try this outUPDATE t1SET t1.department=t2.departmentFROM T1 t1JOIN T2 t2ON t1.userid=LEFT(t2.useremail,CASE WHEN CHARINDEX('@',t2.useremail)>0 THEN CHARINDEX('@',t2.useremail) ELSE LEN(t2.useremail)+1 END -1)
]Thank you visakh16, it solve my problem.Thank to madhivanan too.the query that i used is Select left(useremail,charindex('@',useremail)-1) from t1The error message when i put in the -1 = Invalid length parameter passed to the SUBSTRING function |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-14 : 06:03:51
|
It means you have emails without "@", which is a sign of storing bad data in the table. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 06:16:43
|
quote: Originally posted by Peso It means you have emails without "@", which is a sign of storing bad data in the table. E 12°55'05.63"N 56°04'39.26"
i guessed it right then |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-14 : 06:21:06
|
quote: Originally posted by visakh16
quote: Originally posted by Peso It means you have emails without "@", which is a sign of storing bad data in the table. E 12°55'05.63"N 56°04'39.26"
i guessed it right then 
hahhah, dont know, i guessed it right too, but not working. heheheh |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-15 : 02:29:27
|
quote: Originally posted by visakh16
quote: Originally posted by Peso It means you have emails without "@", which is a sign of storing bad data in the table. E 12°55'05.63"N 56°04'39.26"
i guessed it right then 
Hi all, my problem still there, below is my query.UPDATE t1SET department = tb2.departmentFROM t1 AS tb1 INNER JOIN t2 AS tb2 ON tb1.userid = LEFT(tb2.useremail, CASE WHEN CHARINDEX('@', tb2.useremail) > 0 THEN CHARINDEX('@', tb2.useremail) ELSE LEN(tb2.useremail) + 1 END - 1) CROSS JOIN t1don't know why the mapping is wrong.can any one told me why??? |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-15 : 02:39:24
|
| Can we restart my problem??T1userid department_________________________JEFF NULLJohn NULLPeter NULLT2Email Department__________________________Jeff@domain.com ITJohn@domain.com ResignPeter@domain.com Oldwhat i want is update set T1 to T1userid Department_________________________JEFF ITJoHN Resignpeter oldbut after i run my sql statement belowUPDATE t1SET department = tb2.departmentFROM t1 AS tb1 INNER JOIN t2 AS tb2 ON tb1.userid = LEFT(tb2.email, CASE WHEN CHARINDEX('@', tb2.email) > 0 THEN CHARINDEX('@', tb2.email) ELSE LEN(tb2.email) + 1 END - 1) CROSS JOIN t1my result as below :JEFF ITjohn OLDpeter ITWhat wrong with the query or is there other sql to solve?Thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-15 : 03:17:48
|
| You dont need a cross join. TryUPDATE t1SET department = tb2.departmentFROM t1 AS tb1 INNER JOINt2 AS tb2 ON tb1.userid = LEFT(tb2.email, CASE WHEN CHARINDEX('@', tb2.email) > 0 THEN CHARINDEX('@', tb2.email) ELSE LEN(tb2.email) + 1 END - 1)MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-15 : 03:45:16
|
| calvinkwoo3000,what was the reason for adding CROSS JOIN? |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-15 : 04:31:28
|
quote: Originally posted by visakh16 calvinkwoo3000,what was the reason for adding CROSS JOIN?
i didn't put the cross join, but when i excute in the Sql query, the cross join is added itself |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-15 : 04:33:10
|
quote: Originally posted by calvinkwoo3000
quote: Originally posted by visakh16 calvinkwoo3000,what was the reason for adding CROSS JOIN?
i didn't put the cross join, but when i excute in the Sql query, the cross join is added itself
which tool you're using? |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-15 : 04:36:10
|
| SQl server management studio 2005 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-15 : 04:37:42
|
quote: Originally posted by calvinkwoo3000 SQl server management studio 2005
i'm also using it. but i dont get that cross join appended |
 |
|
|
calvinkwoo3000
Yak Posting Veteran
98 Posts |
Posted - 2008-10-15 : 04:42:53
|
| ???but the cross join appended at mine, how come?!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-15 : 04:47:08
|
quote: Originally posted by calvinkwoo3000 ???but the cross join appended at mine, how come?!!!!
not sure about that. have you installed some thrid party tools along with it? |
 |
|
|
Next Page
|