I have a varchar column in a table that is used to store xml data. Yes, I know that there is an xml data type that I should use, but I think it was set up before the xml data type was available, so varchar is what I should use now. :)
The saved data looks something like this:
<xml filename="100100_456_484351864768.zip" event_dt="10/5/2009 11:42:52 AM"> <info user="TestUser" /> </xml>
I need to parse the file name to get the numbers between the two underscores, which in this case will be β456β. The first part of the file name "should not" vary in length, but the average will be. I need a solution that will work if the first part changes the length (you know that it will change, because βshould not changeβ always means that it will change).
For what I have now, I use XQuery to pull out the file name, because I realized that this is probably better than straightforward manipulation. I passed the xml string for this, but I am not an XQuery expert, so of course I have problems. I found a function for XQuery (a substring before), but could not get it to work (I'm not even sure that the function will work with SQL Server). There may be an XQuery function for this, but if I don't know about it.
So, I get the file name from the table with a query similar to the following:
select CAST(parms as xml).query('data(/xml/@filename)') as p from Table1
From this, I would suggest that I could CAST return back to the string and then execute some instring or charindex function to find out where the underscores are, so that I can encapsulate all of this in a substring, to select which part I need. Without going too far in this, I am pretty sure that in the end I can do it, but I know that there should be an easier way. Thus, a huge unreadable field will be made in the SQL statement, which, even if I moved it to a function, would still get confused to figure out what was happening.
I am sure this is simpler because it seems to be simple string manipulation. Maybe someone can point me in the right direction. Thanks