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.
| Author |
Topic |
|
rajikrishna
Starting Member
12 Posts |
Posted - 2007-01-19 : 07:17:17
|
| Hello,I have a table with values likeF:\\foldername\subfolder\HowTo.docF:\\foldername\subfolder\HowTo.htmlF:\\foldername\subfolder\HowTo.aspxF:\\foldername\subfolder\Sample.pptWhile fetching records from this table If there is a *.doc file then I should display it as HowTo and also same is the case with *.ppt.I need to use Case statments but I am not getting the proper syntax, to find is the value ends with *.doc or *.ppt I have used like '%.doc' but couldnt proceed further.My desired output should beHowToF:\\foldername\subfolder\HowTo.htmlF:\\foldername\subfolder\HowTo.aspxSampleThanks in advance |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-19 : 07:21:47
|
See this topic http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=77795-- prepare sample datadeclare @tbl table (filename varchar(100))insert @tblselect 'peso.txt' union allselect 'peso.asp' union allselect 'harsh.asp'declare @childtable table (extension varchar(10))insert @childtableselect '.txt' union allselect '.mpg'select t.*from @tbl as twhere exists (select null from @childtable as ct where t.filename like '%' + ct.extension) Peter LarssonHelsingborg, Sweden |
 |
|
|
rajikrishna
Starting Member
12 Posts |
Posted - 2007-01-19 : 07:31:50
|
| Hi Peso,Thanks.I saw that topic but I dont have any doubt in finding out if its a .doc or .ppt but I am stammering with case statements.I am not getting them right, never tried case stmts before (learning SQL now). |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-19 : 07:45:58
|
| [code]-- prepare sample datadeclare @f table (filename varchar(200))insert @fselect 'F:\foldername\subfolder\HowTo.doc' union allselect 'F:\foldername\subfolder\HowTo.html' union allselect 'F:\foldername\subfolder\HowTo.aspx' union allselect 'F:\foldername\subfolder\Sample.ppt'-- do the workselect filename, case when right(filename, ext) in ('.doc', '.ppt') then right(filename, sl - 1) else filename endfrom ( select filename, charindex('.', reverse(filename)) as ext, charindex('\', reverse(filename)) as sl from @f ) as d[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-19 : 07:50:35
|
This does the same thing without derived table-- prepare sample datadeclare @f table (filename varchar(200))insert @fselect 'F:\foldername\subfolder\HowTo.doc' union allselect 'F:\foldername\subfolder\HowTo.html' union allselect 'F:\foldername\subfolder\HowTo.aspx' union allselect 'F:\HowToaspx' union allselect 'F:\foldername\subfolder\Sample.ppt'-- do the workselect filename, case when right(filename, charindex('.', reverse(filename))) in ('.doc', '.ppt') then right(filename, charindex('\', reverse(filename)) - 1) else filename endfrom @fPeter LarssonHelsingborg, Sweden |
 |
|
|
rajikrishna
Starting Member
12 Posts |
Posted - 2007-01-19 : 08:08:47
|
| Thank you Peso.Could you please explain whatright(filename, charindex('\', reverse(filename) - 1)deos.In my table I am havin values like HowTo. docsometimes I get a space after the ".".So I am thinking of using like operator.Cant i never use when right(filename, charindex('.', reverse(filename))) like ('%doc','%ppt') then right(filename, charindex('\', reverse(filename) - 1)?? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-19 : 08:13:19
|
| No, not with LIKE operator.If you have problem with spaces, you need to consult your programmer responsible for storing the filenames.No, you will have to figure that out for yourself. How will you ever learn and improve your skills? All commands are documented in Books Online (which should be your best friend).Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|