Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 String manipulation
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

16 Posts

Posted - 10/16/2012 :  11:24:39  Show Profile  Reply with Quote

I have a table called filesnames with 5 colums: FullDate, DistID, Month, Year, and FileName. I first fill the Filename column with files that are in this format AAA_2012-08_Warr.xls or AAA_2012-08_Stmt.pdf. I am trying to extract strings from these filenames to fill the last remaining 4 colums. For example:

Taking file AAA_2012-08_Warr.xls. I would like to extract and fill the columns like below:

FuLLDate: August 2012
Month: 08
Year: 2012

Please help.

Flowing Fount of Yak Knowledge

2875 Posts

Posted - 10/16/2012 :  11:42:23  Show Profile  Reply with Quote
If the filename never deviates from your sample, this will work

DECLARE @str varchar(20)= 'AAA_2012-08_Warr.xls'

PARSENAME(REPLACE(@str,'_','.'),4)as DistID
,RIGHT(PARSENAME(REPLACE(@str,'_','.'),3),2) as [Month]
,LEFT(PARSENAME(REPLACE(@str,'_','.'),3),4) as [Year]
You can derive FullDate from above.


Everyday I learn something that somebody else already knew
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000