Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 13th, 2004, 02:01 PM
Authorized User
 
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Find the frequency of a char(s) in a string

This is a question I could not anwser during the interview. Please help. I want to lean.

How to write a single sql statement that return the frequency of some character(s) in a column? For example, a description of a record is: "Wireless-G is the upcoming 54Mbps wireless networking standard ..." and The interviewer want to know how many 'es' in that string, the frequency of 'es' for each rows( can not use cursor).

Thanks

 
Old October 13th, 2004, 02:37 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I think this has been brought a couple of times on this forum, but if it's for an interview, don't you think it's better you find that out yourself?

What happens if you get the job, and on your first work day your boss ask a similar but slightly different question. Then what? What do you answer him?

I think honesty is best for the long term. If you don't know the answer, say so. It's not that bad if you don't know all the answers. If you're afraid of too many unanswered questions, you're applying for the wrong job ;)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Jane Jane by Golden earring (Track 6 from the album: The hole) What's This?
 
Old October 13th, 2004, 02:44 PM
Authorized User
 
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes I did tell him that I dont know, but I want to lean how to do that.

 
Old October 13th, 2004, 02:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, in that case, tell him there are two "es"'s in that text ;)

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Peter Rauhofer's Doomsday Club by Frankie Goes To Hollywood What's This?
 
Old October 13th, 2004, 03:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Just kidding.

Anyway, I am not sure if that can be done with pure T-SQL. Well, actually, it might be possible, I just don't know how to do it.

I think personally, if I had to do something like this, I'd write a little function that parses the string for the requested characters. This may not be the most efficient way, but it's the only way I know how to do it. (If it has to be done in SQL).

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Vanishing by A Perfect Circle (Track 5 from the album: Thirteenth Step) What's This?
 
Old October 13th, 2004, 03:50 PM
Authorized User
 
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think the same way. He also asked question like: "After deleted records with sql statement in T_SQL environment, and you commited the update, then you recorgnized you deleted the wrong records and He want to rollback( No backup database ). I didnot know the answer too, I failed the interview.

 
Old October 14th, 2004, 01:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

AFAIK, one cannot rollback that once commit has happened.

_________________________
- Vijay G
Strive for Perfection
 
Old October 14th, 2004, 02:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

LEN(@s) - LEN(REPLACE(@s, 'e', ''))

do I get the job? :D
 
Old October 14th, 2004, 02:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
select LEN('testtesttest') - LEN(REPLACE('testtesttest', 'es', ''))
Gives me 6. As per my understanding that should result in 3;). Though a great try. That works perfect for just one character.
Code:
select LEN('testtesttest') - LEN(REPLACE('testtesttest', 'e', ''))
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
char[...] or string.charAt(...) Ibn_Aziz Java Basics 1 June 14th, 2006 07:08 PM
getting the last CHAR of a string kondapally Crystal Reports 0 December 13th, 2004 03:39 PM
Char in VC and String in VB ??? tranhung Visual C++ 1 February 16th, 2004 03:19 PM
Replace or remove the last char from the string sabu21s XSLT 12 January 8th, 2004 05:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.