Wrong conversion from VarChar to Money - sql

Heyo,
I have a problem with a conversion from a Varchar column to Money.
As far as I know, the money data type want a "." as delimiter for cents.
My strings are like this: 796.01. So actually it should work. But the result is - with this example - 79601,00.
I also tried the replace function, ( "," as search_expression and "." as replace_expression) but wihtout success.
I'm using the Derivec Column Task in SSIS with a SQL 2008 R2 Server.
Here is a screen:
Klick Image

Can you try like the given below..[amount] is the input column
(DT_I4)REPLACE(amount,"'","")
It is working for me.
1.Source column is of datatype 'string
After data conversion

If you go to Adavanced Editor for OLE DB source and under Input Output Properties, expand Output Columns, choose required column(for which you want to convert it into currency) and set it's Data Type property as currency [DT_CY] from drop down list. In this way OLEDB source itself will take care of conversion for output column.
There are few indications why it was not working in your case -
Data Conversion - If you are converting data to a date or a datetime data type, the date in the output column is in the ISO format, although the locale preference may specify a different format.
Data Conversion - When copying between columns with a string data type, the two columns must use the same code page.
Derived Column - If an expression references an input column that is overwritten by the Derived Column transformation, the expression uses the original value of the column, not the derived value.

Related

What data type do I use to load scientific (e) notation in SQL? Will eventually load into oracle DB with sqlloader

What data type do I classify these numbers as?
I've tried number, float, float external, and binary double. All of these have given me an invalid datatype error when trying to create a table on SQL to eventually load into.
Assuming your data is currently in string format (and not in number format - if in fact your data is in number format, please see my comment to your question), then you don't need a data type - you need the proper format model to convert strings representing numbers in scientific format into actual numbers.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#BABFJEAA
For example:
SQL> select to_number('1.0e+02', '9.9eeee') as converted_to_number from dual;
CONVERTED_TO_NUMBER
-------------------
100
1 row selected.
This takes the string '1.0e+02' (representing the number 100 in scientific notation) and uses the format model '9.9eeee' - see the linked documentation for this format model.

SQL data type for multiple decimal places

What data type in SQL Server will insert the correct number of decimal places for the number entered into a table. When I try to enter a number like so 6.828.678 into a deciaml or numeric field it says input value was not in the correct format. What data type do i use so that it allows this?
If you are trying to store the data in format 6.828.678 (as in your post) then consider using VARCHAR data type.
You can't store 6.828.678 format data into either deciaml or numeric type field cause they are not valid number format and that's why you are getting the error.
Not sure, but per your comment if you want to SUM and compare the value then store it as INT datatype but before inserting the data make sure to remove the ..
You can use REPLACE() function like CAST(REPLACE('6.828.678','.','') as INT). Hope this helps.

Converting a string to a decimal value in an SSIS package

I have a flat file that I am importing into SQL Server 2008 using an SSIS package.
The file contains a field with a number, followed by a column with a negative sign when the preceding column value is negative.
For instance:
Price Sign
----- -----
9212.00
29.01 -
268.00 -
453.02
I want to load this value into a decimal column on a table.
In order to accomplish this, I am creating a derived column in my Data Flow Task. The expression for the derived column is [Sign] + [Price]. I have (using the Advanced Editor) defined the derived column as type decimal [DT_DECIMAL].
Then I'm simply loading the data (along with the derived column) into my table.
Does this sound like a good way to handle this situation? Is there another method that I should consider instead? Any 'gotchas' with using this method?
Thanks.
Your approach seems to be the best option. The default data type for each column from the flat file is string. So, your [Sign] + [Price] expression is doing string concatenation. And, the data is implicitly converted to a decimal value when moved to your output column.
One change that would improve readability is to explicitly cast the string value to a decimal. You can change your expression in the Derived Column component to this:
(DT_DECIMAL, scale)[Sign] + [Price]
where scale is a number that will match the data type for your output column.
With this change, the output from your Derived Column component will be the decimal data type.
Maybe this will not work...
You need to convert Sign to string data type and Price to numeric.
Then compare if Sign is "-", if is, multiple from -1.
Derived Column Code Example:
(DT_WSTR, 1) [Sign]=="-"? [Price]*-1 : [Price]

How to formulate conditional expression in SSIS Data Flow tab?

I am developing an SSIS 2008 package and I am trying to create a Derived Column transformation. But when I go to the Expression editor and try this expression it gives me alot of errors. I have tried various differentiations of this but all have resulted in errors. I need one of you SQL experts to point out a better expression!
ISNULL(WITHDRAWAL_DATE)||TRIM(WITHDRAWAL_DATE)==""?NULL:CAST(WITHDRAWAL_DATE
AS DATETIME)
So I want this WITHDRAWAL_DATE input String datatype to be compared to an empty string--if it is empty I want it to become Null, otherwise to be cast as a date.
Thanks guys for your helps. I am so confused! WITHDRAWAL_DATE is a DATE data type input in the source XML file and now I have it as a STRING data type in my XSD file. Ultimately the problem is that some of the Withdrawal_Date fields in my XML source data are empty. So I want to insert Null values into my database for these records.
What data type do I need to specify in my XSD, XLST, and SQL output table? And it doesn't matter to me if I use Data Conversion task or Derived Column Xform but since I am new to these, could you send me expression syntax?
#BobS: when I ran with your updated solution, I received error:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
So when I googled datetime2 datatype it looks like this is a new data type that supports larger time/date fields. So I modified my SQL table to use DATETIME2 instead for this field and modified your cast expression below to use DATETIME2 but then output of transform didn't change accordingly.
I also tried changing WITHDRAWAL_DATE to datetime for all files and then changing SQL Table to say NOT NULL for this field. But this also gave me errors.
I see a couple of issues that you should address with this Derived Column transformation.
First, you can't change the data type of a column, which is what your expression is trying to do. I'm not sure if you're actually trying to do this. But, if your output column is the same as the input column, then you will have to change it. To do this, in the Derived Column editor, the Derived Column Name should be a new column name and the Derived Column should be <add as new column>
The expression needs two changes. To assign a NULL value you use a null function with syntax NULL(data-type). And, the CAST function syntax is (DT_datatype)columnname. So, here's how your expression should look
ISNULL(WITHDRAWAL_DATE) || TRIM(WITHDRAWAL_DATE) == "" ? NULL(DT_DBTIMESTAMP) : (DT_DBTIMESTAMP)WITHDRAWAL_DATE
UPDATE: You should be able to use the expression above; but, I did change it to reference the DT_DBTIMESTAMP data type. The SSIS DT_DBTIMESTAMP data type matches the DATETIME SQL Server data type.
To learn what data type you should use for the source component, you can right-click on the source component and select Show Advanced Editor... Select the inputs and outputs tab. Navigate the tree view to find your column and view the associated data type. The Advanced Editor is available most (maybe all) data flow components.
UPDATE 2: IF your output data type for the Derived Column component is DT_DBTIMESTAMP2 instead of DT_DBTIMESTAMP, make sure you change both DT_DBTIMESTAMP references in your expresson. Before closing the Derived Column component, look at the Data Type column for your expression. You can't change it, but it will show the data type that the expression output will be. If it's not what you want, then there's still a problem with your expression.
For, the source files, you can't change the data type of the external columns. At least, I haven't been able to do it. In SSIS, you have to work with what is interpreted by the Source component. If you can alter the files, to change data type, then great. Then, use the Derived Column component to convert what is giving to what you need.
What errors are you getting? I suspect one of them is a cast error.

Does changing the datatype of a column after data is already there destroy the data?

Let's say I have a column of varchar(40) with data already and i change the datatype of that column to integer. does the data change at all in the columns (ie, does the data 'corrupt') or does it not matter and a table of (1,2,3) will still be (1,2,3) regardless of the datatype?
It will be the same, subject to
datatype conversion error (eg "foo" to int)
truncation (eg "foobar" in char(4))
If you attempt to change a column's datatype and the new type is incompatible with the old type, it will fail and nothing will change. You'll get an error like:
"Disallowed implicit conversion from data type <type> to data type <type2>".
Your first problem when you do something like this is that not all the data may meet the criteria to make the change. Those records need to be found and fixed before changing a data type.
The safest way to do something like this is to:
Make a backup
Find and fix any data that will not meet the criteria for the new datatype
Create an additional column in the correct datatype
Migrate the data
Drop the orginal column
Rename the new column to the old name