| Author |
Topic |
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 06:59:58
|
| Good day, I need help. I have a difficult one. I have a filed that returns a unique values but length chages & i only want the last part of the string values after the second "_"Field: ul_633948394629672802_35_Copy of Test1.xls ul_633948409351423738_2_Copy of Test3New Item Test.xlsthe position of the second "_" changes but it always one of the 2 positions being 2 or 1 char in front of it. Then i want to return the part therafter: Copy of Test1.xls & Copy of Test3New Item Test.xlsPlease help - no clue on how to do thisRegards |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-30 : 07:07:14
|
You can use substring(), reverse() and charindex() and again reverse() to the part of the string from the end to the FIRST underscore. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 07:09:17
|
| Thank You for assisting, can you please provide code :( |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 07:13:17
|
| [code]select data,right(data,charindex('_',reverse(data))-1) as [filename] from ( select 'ul_633948394629672802_35_Copy of Test1.xls' as data union all select 'ul_633948409351423738_2_Copy of Test3New Item Test.xls') as t[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 07:16:23
|
| is there a way to do it without the unionbecuase i already have a big statement & this forms part of it & can't do a union for the rest of the query!only one select where i can slot it in my current queryRegards |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 07:18:03
|
quote: Originally posted by ismailc is there a way to do it without the unionbecuase i already have a big statement & this forms part of it & can't do a union for the rest of the query!only one select where i can slot it in my current queryRegards
Post the query you haveMadhivananFailing to plan is Planning to fail |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 07:24:04
|
| IF EXISTS (select * from dbo.fcEventArchive where ProcessID = @ProcessNo)select MAX(Case when v.ControlID = 'VE1020User' then v.ControlValue else '' END) as I_User ,MAX(Case when v.ControlID = 'VE1040Current' then v.ControlValue else '' END) as I_CurrentSupp,MAX(Case when v.ControlID = 'VE10FileINTCreditApp' then v.ControlValue else '' END) as I_INTCreditAppetcfrom dbo.fcEventArchive as e left join dbo.fcEventValueArchive as v on v.EventHistID = e.EventHistID and v.ControlID in ('VE1020User', 'VE1040Current', etc) join (select max(v1.EventHistID) as max_id,v1.ControlID from dbo.fcEventArchive E1 left join dbo.fcEventValueArchive V1 on E1.EventHistID = V1.EventHistID where E1.ProcessID = @ProcessNo group by v1.controlid) max_qry on max_id = V.EventHistID and max_qry.ControlID = V.ControlID where e.ProcessID = @ProcessNoElse select MAX(Case when v.ControlID = 'VE1020User' then v.ControlValue else '' END) as I_User |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 07:30:10
|
| What is the column that you want to apply if for?MadhivananFailing to plan is Planning to fail |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 07:44:13
|
| ,MAX(Case when v.ControlID = 'VE10FileINTCreditApp' then v.ControlValue else '' END) as I_INTCreditApp |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 07:51:52
|
| ,MAX(Case when v.ControlID = 'VE10FileINTCreditApp' then right(v.ControlValue,charindex('_',reverse(data))-1) else '' END) as I_INTCreditAppMadhivananFailing to plan is Planning to fail |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 08:01:45
|
| ,MAX(Case when v.ControlID = 'VE1090INTCreditApp' then right(v.ControlValue,charindex('_',reverse(data))-1) else '' END) as I_INTCreditApp2error: invlaid column name: dataPlease help |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 08:03:01
|
quote: Originally posted by ismailc ,MAX(Case when v.ControlID = 'VE1090INTCreditApp' then right(v.ControlValue,charindex('_',reverse(data))-1) else '' END) as I_INTCreditApp2error: invlaid column name: dataPlease help
,MAX(Case when v.ControlID = 'VE1090INTCreditApp' then right(v.ControlValue,charindex('_',reverse(v.ControlValue))-1) else '' END) as I_INTCreditApp2MadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-30 : 08:13:24
|
Hi madhi, you are able so often to simplify my given approaches.Just to use right() instead of handling substring() - I was blind... cool man! No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 08:23:31
|
| Great stuff - Thank You... it works & i'm gratefull for the help. one ques: what happens if the filename has an "_" within the file name. eg: ul_633948394629672802_35_Copy of_Test1.xlsI think the chances are slim but curious :) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-30 : 08:29:10
|
You have given the rule to extract the part of the string.If that rule is not correct then you have to give a better rule or to live with the results... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 08:33:52
|
| I'm just curious as i said the chances of it happening slim. Thank You very much :) |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 08:59:50
|
| Hi, i get an error invalid string legth specified.i get this error when there is no value returned, value = '',MAX(Case when v.ControlID = 'VE10130EXTVendorApp' then right(v.ControlValue,charindex('_',reverse(v.ControlValue))-1) else '' END) as I_EXTVendorApp |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 09:04:00
|
| ,MAX(Case when v.ControlID = 'VE10130EXTVendorApp' then right(v.ControlValue,charindex('_',reverse(v.ControlValue))-case when charindex('_',reverse(v.ControlValue))>0 then 1 else 0 end) else '' END) as I_EXTVendorAppMadhivananFailing to plan is Planning to fail |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 09:04:42
|
| okay now, added to check for '' in case and v.ControlValue <> '' |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-30 : 09:05:49
|
| Thank You very much :) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 09:05:55
|
quote: Originally posted by webfred Hi madhi, you are able so often to simplify my given approaches.Just to use right() instead of handling substring() - I was blind... cool man! No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks MadhivananFailing to plan is Planning to fail |
 |
|
|
Next Page
|