Converting 9 digit string to number and adding comma - ssis

I need to convert my "Column1" to string numbers to value numbers.
The input is a 9 lenght string like 000011500 or 000002151 or 000000000.
The result should be 115,00 - 21,51 - 0
So first of all I used this Expression with derived column to remove the left zeros:
(DT_WSTR,50)(DT_I8)[Column1]
So my output was 11500, 2151, 0.
Now I want to put comma's from right to the second number.
I was trying to use the following expression with derived column: LEFT([COLUMN1], LEN([COLUMN1]-2)+
"," + RIGHT([COLUMN1],2)
However I am keep getting errors.
Could you please help me?
Thank you!

Why not just convert it to a decimal and divide by 100?
e.g.
((DT_DECIMAL, 2) "000002151") / 100

Related

SQL Query on editing a quantity field

I have a dataset where the values are different, and I want to bring them into a single format.The values are stored as varchar
For ex.
1st Case: 1.23.45 should be 123.45
2nd Case: 125.45 should be 125.45
The first one, has two decimals. I want to remove the first decimal only(if there are 2) else let the value be as it is.
How do I do this?
I tried using replace(Qty,'.',''). But this is removing of them.
I think this can do (although I am not 100% sure about corner cases)
SET Qty = SUBSTRING(Qty, 1, LOCATE(Qty, '.') - 1) + SUBSTRING(Qty, LOCATE(Qty, '.') + 1, LENGTH(Qty) - LOCATE(Qty, '.') - 1)
WHERE LENGTH(Qty) - LENGTH(REPLACE(Qty, '.', '')
You can use a regular expression to handle this case.
Assuming there are only two decimals in your string the below query should be able to handle the case.
select (value,'^(\d+)(\.)?(\d+\.\d+)$',concat('$1','$2')) as a
Here we are matching a regular expression pattern and capturing the following
digits before first decimal occurrence in group one
digits before and after last decimal occurrence including the last decimal in group two.
Following that we are concatenating the two captured groups.
Note that the first decimal has been made optional using ? character and hence we are able to handle both type of cases.
Even if there are more than two decimal cases, I believe a properly constructed regular expression should be able to handle it.

SSIS Flat File add trailing spaces to columns

I am developing a SSIS package which concatenates 3 columns and then outputs the result to a flat file.
1st column is a emp_number consists of length 10.
The values which I get is "12345" or "123456" or "1234567".
In the output I want is "12345 " or "123456 " or "1234567 "
I have a requirement wherein I need to have columns of fixed size(10), so if the length a value for a particular column is lesser than
the expected length I need to pad or fill it with spaces so that the length is matched.
Can you please help.
Add a Derived Column transformation that takes the column value, concatenates it to a string made up of 10 spaces (or whatever the total length after padding should be) and then take the rightmost 10 chars using an expression:
RIGHT("0000000000" + yourcol, 10)
Similar to iamdave's answer but you need the reverse:
left(yourcolumn + " ",10)
There are 10 spaces between the quotes.
if your column is not a string you need to cast it:
left((DT_WSTR,10)yourcolumn + " ",10)

SSIS remove Left zero from csv Flat File Source

I have a csv file that I want to process in SSIS. The file contains a column type string Unicode string [DT_WSTR], example: ColumnA -> ("00000123400").
I want to delete the zeroes that are on the left of 123400 and also delete the quotes and have a result as following: 123400.
For quotation marks I find the following solution via Derived Column: REPLACE (ColumnA, "\" "," "), which gives me the following result: 00000123400.
How to remove the zeroes which are on the left?
After deleting the quotation marks, I tried to convert my string to integer [DT_I4], but that does not remove the zeroes.
Do you have the answer to my case? Thanks in advance.
The solution of a part of the case is:
in our Derived column put the expression:
REPLACE(LTRIM(REPLACE(ColumnA,"0","")),"","0")
It remove just left zero
you can see the link: Removing left padding zero in SSIS
It work perfectly, but is it possible to trim left zero, and also delete quotation marks in the same time in expression?
Example: I have Column1 which is string with quotation marks and left zero - "0000123400"
I try this expression:
REPLACE(REPLACE(LTRIM(REPLACE(column1, "0", " ")), " ", "0"),"\""," ")
but it doesn't work, it deletes all zeros and returns 1234.
The solution that I want is to get 123400.
Should I do it one by one? Create a delivered column and delete quotation marks first, and after create an other delivered column for Left zero ?
Thanks in advance.
It looks like you want the output to be in numeric form? If so, the following expression will remove the quotes and leading zeros while preserving the trailing zeros from Unicode text. This can be done in a single operation, with one Derived Column that will create a new column (add a new column option) with an integer output data type in the data flow.
(DT_I4)REPLACE(ColumnA,"\"","")
If you want to keep this as the Unicode data type the expression below will do this, also in a single Derived Column. Just adjust the length according to your columns.
(DT_WSTR, 50)(DT_I4)REPLACE(CoulmnA,"\"","")

Preserving decimal values in SSIS

I have a column from my .csv file coming in with values as 1754625.24 etc,. where as we have to save it as integer in our database. So am trying to split number with '.' and divide second part with 1000 (24/1000) as i want 3 digit number.
so i get 0.024. But i am having issues storing/preserving that value as a decimal.
I tried (DT_DECIMAL,3) conversion but i get result as '0'.
My idea is to then append '024' part to original first part. So my final result should look like 1754625024
Please help
I am not convinced why would you store 1754625.24 as 1754625024 when storing it as int.
But still for your case , we can use a derived column task and
use Replace command on the source column of csv. E.g.
Replace('1754625.24','.',0)

Finding number of occurence of a specific string in MYSQL

Consider the string "55,33,255,66,55"
I am finding ways to count number of occurence of a specific characters ("55" in this case) in this string using mysql select query.
Currently i am using the below logic to count
select CAST((LENGTH("55,33,255,66,55") - LENGTH(REPLACE("55,33,255,66,55", "55", ""))) / LENGTH("55") AS UNSIGNED)
But the issue with this one is, it counts all occurence of 55 and the result is = 3,
but the desired output is = 2.
Is there any way i can make this work correct? please suggest.
NOTE : "55" is the input we are giving and consider the value "55,33,255,66,55" is from a database field.
Regards,
Balan
You want to match on ',55,', but there's the first and last position to worry about. You can use the trick of adding commas to the frot and back of the input to get around that:
select LENGTH('55,33,255,66,55') + 2 -
LENGTH(REPLACE(CONCAT(',', '55,33,255,66,55', ','), ',55,', 'xxx'))
Returns 2
I've used CONCAT to pre- and post-pend the commas (rather than adding a literal into the text) because I assume you'll be using this on a column not a literal.
Note also these improvements:
Removal of the cast - it is already numeric
By replacing with a string one less in length (ie ',55,' length 4 to 'xxx' length 3), the result doesn't need to be divided - it's already the correct result
2 is added to the length because of the two commas added front and back (no need to use CONCAT to calculate the pre-replace length)
Try this:
select CAST((LENGTH("55,33,255,66,55") + 2 - LENGTH(REPLACE(concat(",","55,33,255,66,55",","), ",55,", ",,"))) / LENGTH("55") AS UNSIGNED)
I would do an sub select in this sub select I would replace every 255 with some other unique signs and them count the new signs and the standing 55's.
If(row = '255') then '1337'
for example.