Trouble with Expression in SSIS Derived Column Transform Editor [duplicate] - visual-studio

This question already has answers here:
SSIS How to get part of a string by separator using Derived Column
(3 answers)
Closed 7 years ago.
I need to split up one column into two columns. The data in the column is currently split by a dash (see below) and has parenthesis between the text.
Example of column: (Data1) - (Data2)
I have a Query that I used against the database that Works, but I am having trouble creating this Expression in SSIS.
Here are the queries I have used in SSMS that generate the New columns without the dash and parenthesis:
To get the data on the left side of the column into a New column:
SELECT SUBSTRING(REPLACE(REPLACE(REPLACE(COLUMN_NAME,'(',''),')',''),' ',''),0,CHARINDEX('-',
REPLACE(REPLACE(REPLACE(COLUMN_NAME,'(',''),')',''),' ',''))) AS NEW_COLUMN_NAME
FROM TABLE
To get the data on the right of the column into a New column:
SELECT SUBSTRING(REPLACE(REPLACE(REPLACE(COLUMN_NAME,'(',''),')',''),' ',''),CHARINDEX('-',
REPLACE(REPLACE(REPLACE(STREKNING_NAVN,'(',''),')',''),' ',''))+1,LEN(REPLACE(REPLACE(REPLACE(COLUMN_NAME,'(',''),')',''),' ',''))) AS NEW_COLUMN_NAME
FROM TABLE
Can anyone offer any tips?
Thank you in advance.

I prefer to solve string manipulation problems with a script task because the code is much more robust and clean than anything that can be done in SQL or SSIS expressions. So it would go something like this:
Add a Script Task
Add two string columns (i.e. "Column1" and "Column2")
Do the following in a script task (assuming input column is named "col":
using System.Text.RegularExpressions;
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string[] columns = Row.col.ToString().Split('-');
Regex reg = new Regex(#"[\(\)]");
Row.Column1 = reg.Replace(columns[0], "");
Row.Column2 = reg.Replace(columns[1], "");
}
So the split function converts the string to an array and we can use regex to remove the parentheses.

Related

Prepared statements with TBS

I'm trying to merge the results of a prepared statement to TBS. Here's my code:
$s = $link->prepare("SELECT * FROM newsletters WHERE newsletter_id = :newsletter_id");
$s->bindParam(":newsletter_id",$newsletter_id);
$s->execute();
$newsletter = $s->fetch(PDO::FETCH_ASSOC);
$tbs->MergeBlock('$newsletter ', $newsletter );
But I can't get the results fields. I get errors like the following:
TinyButStrong Error in field [newsletter.title...]: item 'title' is not an existing key in the array.
I can't find my error.
MergeBlock() is for merging a recordset , so you should use $s->fetchAll() instead of $s->fetch(). The section of the template will be repeated for each record.
But if you have to merge a standalone record, use MergeField() instead of MergeBlock(). The single fields will be merged one by one without repeating.

LINQ to SQL to find match in results that have multiple pipe separated values

I have a table that has multiple columns, one of which contains pipe separated values.
I found an answer that's partially what I'm looking for here but that assumes you're searching in one CSV-type list.
What I have is rows of data, one column (called serviceIDs) has data like 2|45|5|6
I want to be able to pass a value into a query something like this:
Select all rows where serviceIDs contains '5'
or
Select all rows where serviceIDs like '%5%'
But obviously neither of those are going to work properly. Is there a way to do this in LINQ?
Unfortunately Split isn't supported in LINQ to SQL. You could do something like the following where you tack on a leading and trailing pipe and then use contains with the leading and trailing pipe. That would solve the issue where finding 45 when searching for 5. Be aware that this could kill your performance because the calculated column value would block index usage in the database. If you store the original values with the leading and trailing pipe, the indexing would be better (but not as good as using a full text search).
var yourvalue = "5";
var expectedResult = "|" + yourvalue + "|";
var resultset = rows.Where(row => ("|" + row.serviceIDs + "|").Contains(expectedResult);
var yourvalue = "5";
var resultset = rows.Where(row => row.serviceIDs.Split('|').Contains(yourvalue));
disclaimer: i never used linq for DB stuff, but something along these lines might work
yourTable.AsEnumerable()
.Select(row => row["serviceIDs"].ToString().Split('|'))
.ToList()
.Where(s => s == 5);

create a bindingsource that supports advanced sorting from IOrderedQueryable

I want to create a bindingSource that supports sorting by multiple columns and attach it to a bindingNavigator.
I am using winforms and code-first EF.
I am constructing my query in the following manner
DbSet<Person> dset = DBContext.People;
string Searchstr ="Smith";
string sortby ="LastName, FirstName"
var qry = (IOrderedQueryable<Person>) dset.Where(
p => p.LastName.Contains(Searchstr) )
.OrderBy(sortby);
binding.datasource = dset.Local.ToBindingList(); // where binding has been dropped on form at design time.
binding.sort = sortby; // this is the line that screws up the sort order
I am using the extension method documented here to achieve the multi-column order by.
The problem turned out to be that I had set binding.sortby =sortby.
Using the multi column sortby was OK in the query because of the clever extension ( see link in the question) however it does not work to set the binding.sort to an expression involving multiple columns
The code works fine if I remove that line.

Getting value of database field in Crystal Reports

Currently I'm working on legacy application, that uses crystal report engine. I have to get value of database fields programmatically. As I've assumed, I need proper event for getting next code to work:
Report.Database.Tables(1).Fields(1).Value
But the value is always empty in DownloadStarted/Finished event handlers. What I'm doing wrong and is it at least possible?
I think that if you want to get value of your table fields in program the best way is that you get the field name from report and then connect to your table directly and use report field names as the table columns name
i do it in c# i hope it can help you in vb6 too:
string name = report2.Database.Tables[1].Fields[1].Name;
string[] names = name.Split('.');
and then add your database to your program and use names like this:
DataTable dt = new DataTable();
string[] value = dt.Columns[names[1]];
if you just need your tables values, you can use my last answer, but if you need value of database fields in crystal report, i mean something like formula field ,this code can help you:
CRAXDRT.FormulaFieldDefinitions definitions = report2.FormulaFields;
string formulaText = "IF " + report2.Database.Tables[1].Fields[3].Name
+ " > 10 THEN" + report2.Database.Tables[1].Fields[2].Name;
definitions.Add("Test", formulaText);
report2.Sections[1].AddFieldObject(definitions[1], 0, 0);

How do I perform a dynamic select in Linq?

I am trying to figure out how to dynamically specify the properties for my select clause in a linq query.
Lets say I have a collection of employee objects. At run time, the end user will be specifying which properties they would like to see for those employees, so I need to be able to dynamically construct my Linq select clause.
I have used the dynamic Linq library, but I prefer not to use that, because it requires me to build a string to pass to the select method. I'd like to understand how to do this via Expressions.
This looks like something that fits more with your requirements of not using dynamic linq.
Use Reflection to get the dynamic Column Values
//columns variable has column name as comma separated String which you
can save in DB //example string columns ="Name,Id,Age";
var strColumns =columns.split(,);
foreach(var myObject in MyObjectcollection)
{
for(int index =0;index<strColumns.count();index++)
{
//Create a collection of objects
mycollection.add(myObject.GetType().GetProperty(strColumns[index]).GetValue(myObject, null));
}
}

Resources