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
 General SQL Server Forums
 New to SQL Server Programming
 Need help in writing query

Author  Topic 

soori457
Yak Posting Veteran

85 Posts

Posted - 2009-11-23 : 01:27:24
Hai All,

I have two tables student and email_extension (only email extensions will be stored in this table)

student(Table)
student_id email
101 suresh@ucf.com
102 kumar@sc.com
103 ganesh@sch.com
104 ramesh@gmail.com

email_extension(Table)
email_exntsnion_id email_extension
1 ucf.com
2 gmail.com
3 yahoo.com
4 sch.com

Now I need to retrieve records where email (email extensions) from student table matches with email_extensions from email_extension table

Thanks in Advance

Suresh Kumar

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2009-11-23 : 02:18:50
SELECT * FROM Student WHERE SUBSTRING(email,CHARINDEX('@',email)+1,LEN(email)) IN (SELECT email_extension FROM Email_extension)
Go to Top of Page

soori457
Yak Posting Veteran

85 Posts

Posted - 2009-11-23 : 02:48:13
Is this correct way?
can we make string operations or any other operations to column in where condition like above query?



Suresh Kumar
Go to Top of Page

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-11-23 : 06:15:04
quote:
Originally posted by lionofdezert

SELECT * FROM Student WHERE SUBSTRING(email,CHARINDEX('@',email)+1,LEN(email)) IN (SELECT email_extension FROM Email_extension)



Great job..

I was trying for this for the past 1 hour.. Good..
Keep it up..

Wishes

Balaji.K
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-11-23 : 06:45:03
Or you could use a join.



DECLARE @student TABLE (
student_ID VARCHAR(14) PRIMARY KEY
, firstname VARCHAR(255)
, surname VARCHAR(255)
, email VARCHAR(255)
)

DECLARE @email_extension TABLE (
email_extension VARCHAR(255) PRIMARY KEY
)

INSERT @student (student_Id, firstname, surname, email)
SELECT 'SG0001', 'fred', 'flintstone', 'fred@bedrock.com'
UNION SELECT 'SG0002', 'wilma', 'flintstone', 'wilma@bedrock.com'
UNION SELECT 'JJ0345', 'George', 'Jetson', 'jetsonG@SpacelySpaceSprockets.co.jup.u'
UNION SELECT 'SBD023', 'Scooby', 'Doo', 'scooby@mysteryMachine.org'

INSERT @email_extension (email_extension)
SELECT 'bedrock.com'
UNION SELECT 'MysteryMachine.org'

SELECT
s.*
FROM
@student s
JOIN @email_extension ee ON s.email LIKE '%@' + ee.email_extension



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -