How to achieve similar datatype - ssis

Goal:
Combine two column named first and lastname, from the same table A and then transfer it to column fullname in table B from a another
relational database.
Column first and lastname has the same datatype as fullname. The datatype is varchar(50) or varchar(100).
Problem:
I can't make the transaction to have the same datatype

You need to use the type cast expression DT_STR in Derived Column transformation so that the output from Derived Column transformation is still in varchar data type.
Below shown Derived Column Transformation shows two new columns.
First new column FullName takes in two input columns FirstName and LastName. Concatenates the columns with a space to separate them and then type casts to DT_STR. In (DT_STR, 100, 1252), 100 represents the length of the output column, 1252 represents the code page.
Second new column FullNameNoCast simply concatentates the two input columns FirstName and LastName. This will result in Unicode data type.
Since, you mentioned that your destination is of varchar data type. I believe that you are not type casting the new column in Derived Column transformation. That might lead to the error you are facing.
Hope that helps.

Related

Is it possible to query an integer range within a string field?

Say I have a simple mysql table containing a name field which is just a varchar. The name field contains a string of the following format. "channelname,unix_timestamp,unix_timestamp". e.g. "bbc1,123456789,123456889". I need to select all rows, where a channelname matches, and where a given timestamp falls within the range of the 2 timestamps. For example, given the timestamp 123456800, and the channelname 'bbc1' The above record should be selected.
How I would accomplish this is to first select all records with "name like 'bbc1,%' split out the two timestamp fields in the calling code, and filter the results there to those containing the given timestamp. Is there a better, more efficient way. My DB could have a very large number of records which match "name like 'bbc1,%'", and it's only expected to grow as time goes on.
I unfortunately don't have the ability to alter the table to add the two timestamp fields, the only thing I have to go on is that single name field. It's also possible that the name field may contain some arbitrary string not of the given format for some records, however all records which start with the given channel name should match this format.

MySQL used ALL Type while searching on Primary Key

My table schema:
My above table has ~10L data. While using EXPLAIN, it shows as,
From this, type shows ALL, Extra shows Using where and rows not in O(1). But, for searching on primary key, the type should be const, rows be in O(1) ?? I can't able to figure out the issue, which results in slowing the queries.
Your id field is varchar while you pass the value you are looking for as a number.
This means that mysql has to perform an implicit data conversion and will not be able to use the index for looking up the value:
For comparisons of a string column with a number, MySQL cannot use an
index on the column to look up the value quickly. If str_col is an
indexed string column, the index cannot be used when performing the
lookup in the following statement:
SELECT * FROM tbl_name WHERE str_col=1;
The reason for this is that
there are many different strings that may convert to the value 1, such
as '1', ' 1', or '1a'.
Either convert your id field to number or pass the value as string.
As your id column is varchar, you need to provide it String while searching.
Try, id= '123456'
Reason:
Since you are comparing a varchar column to Int, it will first convert all rows to Int, and then match it with 123456(int).

Can I create a mapping from interger values in a column to the text values they represent in sql?

I have a table full of traffic accident data with column headers such as 'Vehicle_Manoeuvre' which contains integers for example 13 represents the vehicle manoeuvre which caused the accident was 'overtaking moving vehicle'.
I know the mappings from integers to text as I have a (quite large) excel file with this data.
An example of what I want to know is percentage of the accidents involved this type of manoeuvre but I don't want to have to open the excel file and find the mappings of integers to text every time I write a query.
I could manually change the integers of all the columns (write query with all the possible mappings of each column, add them as new column, then delete the orginial columns) but this sould take a long time.
Is it possible to create some type of variable (like an array with first column as integers and second column with the mapped text) that SQL could use to understand how text relates to the integers allowing me to write a query below:
SELECT COUNT(Vehicle_Manoeuvre) FROM traffictable WHERE Vehicle_Manoeuvre='overtaking moving vehicle';
rather than:
SELECT COUNT(Vehicle_Manoeuvre) FROM traffictable WHERE Vehicle_Manoeuvre=13;
even though the data in the table is still in integer form?
You would do this with a Maneeuvres reference table:
create table Manoeuvres (
ManoeuvreId int primary key,
Name varchar(255) unique
);
insert into Manoeuvres(ManoeuvreId, Name)
values (13, 'Overtaking');
You might even have such a table already, if you know that 13 has a special meaning.
Then use a join:
SELECT COUNT(*)
FROM traffictable tt JOIN
Manoeuvres m
ON tt.Vehicle_Manoeuvre = m.ManoeuvreId
WHERE m.name = 'Overtaking';

How to check each row in a table for at least one null or constant value in SSIS?

I have a table with 20 fields.
1) Is there any way to check all fields if any of them are NULL?
2) For all columns that are nvarchar i want to search if they contain a constant string value e.g. "Missing".
Can i do the above in SSIS without having to check the columns one by one?
There is no Null-inator that would check if any column is null in a row. However, you could concatenate all the columns in the row and if any column is null then the result will be null. So in a derived column task it could look like this:
Field1 + Field2 + (DT_WSTR, 50)Field3...
As demonstrated in field3, all fields that are not string would need to converted.
Similarly, you can find a keyword, like "missing" by concatenating all the fields in a derived column task and using FINDSTRING(). That could looks like this:
FINDSTRING(Field1 + Field2, "Missing",1)
If the value is greater than 0, you have a hit. There a drawbacks though. Given that your columns are nullable, you would need null handling on all the columns unless this test was only performed on rows where there are no nulls. Also, it doesn't tell you which column has the value, so that probably is not very useful unless you are rejecting or quarantining the entire row.

Computed Column Specification Based on Primary Key

I have a field that I want to compute based on a string and the ID generated when a record is inserted. Basically when a record is save with ID = 1, I need the computed field to read 'string_1' and so on. I am trying this is my formula (('PV'+'_')+ID) where PV is the string and ID is the primary key field in the same row as the data inserted but I'm getting a formula error. If I add quotes around ID then I just get PV_ID which is wrong. Any idea how I can reference the ID field in my formula and fetch the value?
here is my table row structure(ID,Computedfield,data1,data2). i need computedfield to have the value of the ID field concatenated with a string. any help appreciated
EDIT
Using SQL SERVER 2008 R2 Standard
Your question isn't totally clear on whether that prefix string is a string literal, or the contents of another column.
If it's a literal, you should be able to say:
ALTER TABLE dbo.YourTable
ADD ComputedColumn AS 'PV_' + CAST(ID AS VARCHAR(10)) PERSISTED
If it's a string contained in another column, you should be able to define it like this
ALTER TABLE dbo.YourTable
ADD ComputedColumn AS PV + '_' + CAST(ID AS VARCHAR(10)) PERSISTED
assuming PV is the column (of type VARCHAR) containing the prefix string.
The main point is: since you're mixing a literal string, and an INT value, you need to CAST the INT to a string first before being able to concatenate those two.
Use formula:
('PV_'+CAST(ID as varchar))
if you want to keep the resulting value - add the PERSISTED in the end