I would like to know a thing if thst is possible.. let's put that I have a database with 3 column (name, last name and age) that obviously will be type: varchar, varchar and int.
then on visual studio I create a datagridrow with a dataset for automatically retrieve values from database so in my grid I will see this:
Mark //Name
Wally //Last name
38 //Age
And untill here all is easy, but if for some reason I change the value of age from int to varchar and add a vale like: "December" when I open my datagrid I should see
Mark //Name
Wally //Last name
December //Age converted from int to varchar only for this example
Instead I get an exception that says: "impossible to store a String in the column 'Age' because type is int", So for fixing this problem a person has got only 2 chances
1) Delete the Dataset on the form designer and create back another one;
2) Going in the code behind of the dataset (DataSet.Designer) and manually change all the values of the column Age from int to varchar.
Both are boring solutions anyway, is it possible isn't there a way to automatically update the dataset after that a changed is made inside the database?
Ty very much guys and Merry Christmas.
Related
I am creating a table with several columns in sql:
CREATE TABLE.....
and one of them is going to have values like this: 675763582022462206:57. As you see it has : in it. So what is a type of it? Is it UInt16 or String?
It must be varchar or nvarchar in this case. The database doesn't recognize ":" as a part of a number, unless you say to Windows in advanced region settings that this is your decimal point. If you can store 57 (after ":") in a different column, then you can save the number before ":" as a bigint if you wish
This value can't be stored in a numeric type due to the colon (:), so you'll have to use one of the character types - i.e., a sufficiently long char or varchar.
I need store exactly numeric data in database.
Let say have to save 123.200 or 123.1 exactly into database.
But result will come up 123.20 or 123.10 in database if column type set to decimal with fixed 2 digit.
What I can do if I just want 123.200 or 132.1 shown on database/report?
No need system auto convert to any other decimal.
You can store the value "as is" in the varchar type.
The problem with this approach is that database would allow to store any string there, even if it is not a number, say 10abc.xyz23
If you need to know how to present the number to the user, you need to store this information somehow. Since, each number in the column may be formatted differently, you need to store this formatting information for each row.
I'd store it as decimal type with large enough scale and precision to cover all possible ranges of your data and in addition to that have extra column DecimalPlaces, which would contain the number of decimal places your reporting engine should use when displaying the value.
If you must do this, then as others have suggested, you'll need to use a character data type to store it. I'd also add a computed column that makes the numeric value readily available also:
create table T (
Val varchar(39) not null,
Val_numeric as CONVERT(decimal(38,10),Val) persisted
)
go
insert into T(Val) values
('123.200'),
('123.1')
select * from T
Results:
Val Val_numeric
--------------------------------------- ---------------------------------------
123.200 123.2000000000
123.1 123.1000000000
When you need the "user entered" value, you use Val. When you need the real value, you use Val_Numeric. This also has the advantage that (without a complex check constraint), you cannot enter any invalid values into the Val column. E.g.:
insert into T(Val) values ('1.2.3')
Produces the error:
Msg 8114, Level 16, State 5, Line 12
Error converting data type varchar to numeric.
I'm trying to get a percentage to display as a decimal in my database.
I have the following set up to convert the percentage columns into decimals:
---------------- ---------------- ------------
excel source ---------> data conversion ----------> db output
---------------- ---------------- ------------
I've tried to strictly convert the input to decimal and numeric.
Neither of these have changed my results.
In my columns in the database I'm getting just 0's and 1's.
Forgive my crude drawing; I do not have enough rep to post pictures yet.
Hope this is what you are looking for
Excel sheet like this is the source.
I just tested it in my system.It is working fine. This is what I did.
Created an SSIS package with just 1 DFT.
Data flow is given below. Please note that the value which appeared as 40% in Excel sheet is visible as 0.40. So I added two derived columns. One converting as such and the next which multiplies with 100.
the derived column structure is shown below.
The destination table structure be
Create table Destination
(
id int,
name varchar(15),
hike decimal(8,2)
)
I am getting the result as expected.
Select * from Destination
There are many ways to accomplish this. Here's one:
1) Save your excel file as a tab delimited text file.
2) Create a New Flat File Connection in SSIS
a) Set File Name = .txt file
b) Go to Advanced tab and click on the column with the percentages
c) Set the Data Type to match the target field in your database (e.g., numeric(10,5)
3) In the SSIS workflow, create a derived column of your percent field to convert from percent to decimal(e.g., newfield = oldfield/100). Make sure to check the data type has not changed in the Derived Column Transformation Editor.
I've got a problem when trying to update an sql record, taking the value from DataGridView after the user alter the cell value. Inside the table the value to be modified is a decimal(18,8), when I try to insert a value such as 50.55 inside the DataGridView, then is updated inside the Sql table as 5055.00.
How should I proceed?
Try setting the ValueType for the row and you should be good to go! For example, in your SQL table, the column decimal(18,8) is a number that has 10 digits before the decimal and 8 digits after the decimal thus you need to format your DGV columns as follows (as an example):
DataGridView1.Columns.Item(3).DefaultCellStyle.Format = "N8"
DataGridView1.Columns.Item(3).ValueType = GetType(Decimal)
For more info read this article
Take a sqlCommand in a source item:
SELECT SUBSTRING(MKGDiagnoses.ICD9Code, 2,7) AS ICD9Code
FROM ...
The column ICD9Code outputs a string with a length of 7 positions.
Now change to:
SELECT SUBSTRING(MKGDiagnoses.ICD9Code, 2,6) AS ICD9Code
FROM ...
So datatype of the column is changed to a string of 6 positions.
A change like this is never reflected in the datatype of the output columns, while the datatype of the external column did change to [DT_STR] with length 6.
Is this expected behaviour? Is this behaviour overwritable?
Expected? Sort of? I can't find an earlier answer from me on this but the basics are that the metadata gets set when the item is first created. This is an expensive operation so the designer tries to limit the number of times it must referesh the metadata and so changing the precision of a numeric or increasing/decreasing the length of a string generally isn't a "big enough" change to force a metadata refresh request.
If I changed a single column as you're demonstrating, my lazy hack would be to rename the column in my source query (and then name it back).
Original
SELECT SUBSTRING(MKGDiagnoses.ICD9Code, 2,7) AS ICD9Code
FROM ...
Temporary
SELECT SUBSTRING(MKGDiagnoses.ICD9Code, 2,6) AS ICD9CodeShort
FROM ...
Revert
SELECT SUBSTRING(MKGDiagnoses.ICD9Code, 2,6) AS ICD9Code
FROM ...
If you have lots of columns to fix, I usually make the query SELECT 1 AS foo and then re-open the editor and use my real source query.