|
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
|
|
|
October 13th, 2004, 02:01 PM
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
October 13th, 2004, 02:37 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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?
|
October 13th, 2004, 02:44 PM
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes I did tell him that I dont know, but I want to lean how to do that.
|
October 13th, 2004, 02:56 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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?
|
October 13th, 2004, 03:34 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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?
|
October 13th, 2004, 03:50 PM
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
October 14th, 2004, 01:52 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
AFAIK, one cannot rollback that once commit has happened.
_________________________
- Vijay G
Strive for Perfection
|
October 14th, 2004, 02:41 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
LEN(@s) - LEN(REPLACE(@s, 'e', ''))
do I get the job? :D
|
October 14th, 2004, 02:47 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|
|