Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to extract the paricular values from a string


How to extract the paricular values from a string

Author
Message
sqlstud
sqlstud
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 237
Hi,

We have the below file names

File Name1 = 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
File Name2 = 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT

Query:

Need to extract the file names as below

File Name1 = XX_YYYYY_AA_BBB_SampleTransaction
File Name2 = XX_YYYYYY_DDD_MasterTransaction

The values after the first underscore from right(_55322) will be increasing from file to file, so we cant use the below query

SELECT SUBSTRING(@FILENAME,1,(LEN(@FILENAME)-16))

Could you please let me know the query to get the correct result?

Regards
SqlStud
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16445 Visits: 13202
You can reverse the string, look for the position of the second underscore and take the substring starting from that position till the end. Reverse the substring back and you have your result.

edit: or don't reverse the string and look for the fourth underscore, if the first part keeps the same structure.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
Here are two ways:


WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=SUBSTRING(fn, 1, CHARINDEX('.', fn))
FROM Filenames;

WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=Item
FROM Filenames
CROSS APPLY PatternSplitCM(fn, '[0-9a-zA-Z_]')
WHERE [Matched] = 1 AND ItemNumber = 1;




The second uses PatternSplitCM, which is a general tool that can be used for this purpose, and is described in the 4th article in my signature (splitting strings on patterns). That article also shows how to split out other components of a file name.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
sqlstud
sqlstud
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 237
Koen Verbeeck (1/8/2013)
You can reverse the string, look for the position of the second underscore and take the substring starting from that position till the end. Reverse the substring back and you have your result.

edit: or don't reverse the string and look for the fourth underscore, if the first part keeps the same structure.


Thanks SScrazy.

Can you please send me the query?

Regards
SqlStud
sqlstud
sqlstud
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 237
dwain.c (1/8/2013)
Here are two ways:


WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=SUBSTRING(fn, 1, CHARINDEX('.', fn))
FROM Filenames;

WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=Item
FROM Filenames
CROSS APPLY PatternSplitCM(fn, '[0-9a-zA-Z_]')
WHERE [Matched] = 1 AND ItemNumber = 1;




The second uses PatternSplitCM, which is a general tool that can be used for this purpose, and is described in the 4th article in my signature (splitting strings on patterns). That article also shows how to split out other components of a file name.


Thanks dwain.c

using the first approach, the result is like

XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.
XX_YYYYYY_DDD_MasterTransaction_120807_00005.

But i need the below output

XX_YYYYY_AA_BBB_SampleTransaction
XX_YYYYYY_DDD_MasterTransaction

Please suggest on this..

Regards
SqlStud
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
sqlstud (1/8/2013)
dwain.c (1/8/2013)
Here are two ways:


WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=SUBSTRING(fn, 1, CHARINDEX('.', fn))
FROM Filenames;

WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=Item
FROM Filenames
CROSS APPLY PatternSplitCM(fn, '[0-9a-zA-Z_]')
WHERE [Matched] = 1 AND ItemNumber = 1;




The second uses PatternSplitCM, which is a general tool that can be used for this purpose, and is described in the 4th article in my signature (splitting strings on patterns). That article also shows how to split out other components of a file name.


Thanks dwain.c

using the first approach, the result is like

XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.
XX_YYYYYY_DDD_MasterTransaction_120807_00005.

But i need the below output

XX_YYYYY_AA_BBB_SampleTransaction
XX_YYYYYY_DDD_MasterTransaction

Please suggest on this..

Regards
SqlStud


Sorry about that! Misread the requirement. Try this:


WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=SUBSTRING(fn, 1, PATINDEX('%[0-9]%', fn)-2)
FROM Filenames;




The PatternSplitCM will work too (not as is) but it would need to be changed into something quite a bit more complicated.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
Or, even simpler:


WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=LEFT(fn, PATINDEX('%[0-9]%', fn)-2)
FROM Filenames;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
sqlstud
sqlstud
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 237
dwain.c (1/8/2013)
Or, even simpler:


WITH Filenames (fn) AS (
SELECT 'XX_YYYYY_AA_BBB_SampleTransaction_120807_55322.TXT'
UNION ALL SELECT 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT')
SELECT fn=LEFT(fn, PATINDEX('%[0-9]%', fn)-2)
FROM Filenames;





Thanks a lot Dwain.

Its working...

declare @a varchar(100) = 'XX_YYYYYY_DDD_MasterTransaction_120807_00005.TXT'
select LEFT(@A, patindex('%_[0-9]%', @a)-1)


Regards
SqlStud
sqlstud
sqlstud
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 237
Koen Verbeeck (1/8/2013)
You can reverse the string, look for the position of the second underscore and take the substring starting from that position till the end. Reverse the substring back and you have your result.

edit: or don't reverse the string and look for the fourth underscore, if the first part keeps the same structure.


Thanks Koen
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search