SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 String manipulation
 New Topic  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

2870 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  
 New 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.03 seconds. Powered By: Snitz Forums 2000