Source column is varchar datatype with null values and need to loaded into date column with default values for nulls in the column. SSIS
I tried to convert using REPLACEISNULL but it didn't work.
Please suggest me.
Thanks
My first choice would be to use a SQL query for the source, and doing ISNULL() or COALESCE() to replace NULL with a default value.
If you have to do it later in the dataflow, you can use the ternary operator in a Derived Column Transformation:
MyColumn == null ? 'default value' : MyColumn
And of course, cast the result of the expression as a date datatype.
Related
I tried changing the datatype from Varchar to Datetime, but it keeps on bringing the following error.
Incorrect datetime value: ' ' for column 'pickup_time' at row 6
Result
I was expecting for the query in the image to work.
To change a type to datetime, all existing values must be able to be cast as datetimes without any error. Find the values that are not, or update them first using str_to_date if it is simply a matter of changing the format.
If there are blank values, you will need to change them to null first.
I'm creating a calculated field (Field3) in a query in MS Access. In this example, Field2 contains both numeric and character values. I want Field3 to contain only numeric values from Field2 and to convert all character values to Null values so that I can later perform calculations on Field3 as a numeric field. This is in an IIf function because I want Field3 to contain only values from Field2 if Field1 = "AA". This is what I tried typing in the Field row in the Query Design View:
Field3: IIf([Field1]="AA",[Field2]*1,NULL)
This works except where Field2 is a character value then Field3 reads "#Error" instead of being blank.
What is the proper syntax for assigning NULL values if the IIf condition is not met?
If you want the expression to return [Field2]*1 only when [Field1]="AA" and [Field2] contains a number, but otherwise return Null, use IsNumeric() in the condition ...
IIf([Field1]='AA' And IsNumeric([Field2]), [Field2]*1, Null)
If the purpose of [Field2]*1 is to cast the [Field2] text value as a number, consider the Val() function instead ...
IIf([Field1]='AA' And IsNumeric([Field2]), Val([Field2]), Null)
Null is the correct syntax.
But at least in the old German Access version which I have on this machine, I need to use semicolons instead of commas in the Query Design View.
So can you try this?
Field3: IIf([Field1]="AA";[Field2]*1;NULL)
In SQL View, the syntax is exactly like in your question:
SELECT IIf([Field1]="aa",[Field2]*1,Null) AS Field3
FROM TestTable;
Good Morning All;
I currently have a MySQL table where there are 3 date fields (Columns) that were loaded as strings in this format 20140101 YYYYmmdd. I would like to convert this to a date format 2014/01/01 YYYY/mm/dd. Can someone please provide a simple sql syntax that would alter the table to a date format from a string and change the column to display the dates like this 2014/01/01 and not like 20140101. Thanks to all
Try this:
date_format(str_to_date(datecolumn, '%Y%m%d'),'%Y/%m/%d')
If you just want to reformat the values in the VARCHAR column, assuming that the column with sufficient length e.g. VARCHAR(10), and all the values are eight characters in length...
You could do something like this:
UPDATE mytable t
SET t.mycol = CONCAT( LEFT( t.mycol ,4)
, '/'
, SUBSTR( t.mycol ,5,2)
,'/'
, SUBSTR( t.mycol ,7,2)
)
WHERE CHAR_LENGTH(t.mycol) = 8
We want something in the statement that will prevent the statement from "working" a second time, if it's inadvertently re-run. It doesn't have to be CHAR_LENGTH. We might want to include a check that the value doesn't already contain a slash character AND t.mycol NOT LIKE '%/%'.
But why on earth are "date" values being stored in character columns, rather than in DATE datatype, which is custom designed for storing and working with date values?
ALTER TABLE mytable MODIFY mycol DATE ... ;
(If the column is defined as NOT NULL, has a default value, has a comment, those attributes can be retained, they need to be included in the new column specification, e.g.
ALTER TABLE mytable MODIFY mycol DATE NOT NULL COMMENT 'creation date';
Note that DATE columns do not have a "format" per se. When converting to string, MySQL uses date format '%Y-%m-%d'. And MySQL expects string literals representing date values to be in that same format. To get a value from a DATE column converted to string in format 'yyyy/mm/dd'.
SELECT DATE_FORMAT(date_col,'%Y/%m/%d') AS date_col
To get a string value in that format converted to DATE datatype
SELECT STR_TO_DATE('2015/06/01','%Y/%m/%d')
I have 2 columns startdate and enddate of type int. These columns are used to store timestamp data.
Now I have to extract the date component from this timestamp, convert it back to timestamp an store it in another column startdate1 of type int
But on doing this, I get a warning 'Data truncated for column startDate1 at row'.
The sql queries are:-
ALTER TABLE `ServiceRule` ADD COLUMN `startDate1` INT(11) NULL DEFAULT NULL AFTER `endDate` , ADD COLUMN `endDate1` INT(11) NULL DEFAULT NULL AFTER `startDate1`;
update `ServiceRule` set `startDate1`= TIMESTAMP(DATE(from_unixtime(`startDate`)));
update `ServiceRule` set `endDate1`= TIMESTAMP(DATE(from_unixtime(`endDate`)));
Now if i change the datatype of startDate1 and endDate1 to TIMESTAMP, the first update query of startDate1 runs successfully.
But the endDate1 update query shows the warning 'Out of range value for column 'endDate1' at row'.
After browsing for solution, i got know that this occurs if the input value is greater than the column datatype range.
Can anybody please try to help me out?
Thanks in Advance. :)
I think what you want is UNIX_TIMESTAMP instead of TIMESTAMP.
UNIX_TIMESTAMP is the inverse function of FROM_UNIXTIME.
What means that one of the rows contains a value that cannot be converted to int because indeed it is either too more or too less than expected.
Can't you just convert the columns to timestamp and do the extraction from there? Try to query per set of 100 for example and narrow down the faulting row.
When the field startDate is of type int, then alter table statement used is not correct to achieve what you wanted.
Change it as below:
-- keeping the added fields as is, execute the following
ALTER TABLE `ServiceRule` MODIFY COLUMN `startDate1` DATETIME DEFAULT NULL;
ALTER TABLE `ServiceRule` MODIFY COLUMN `endDate1` DATETIME DEFAULT NULL;
update `ServiceRule` set `startDate1`= TIMESTAMP(DATE(from_unixtime(`startDate`)));
update `ServiceRule` set `endDate1`= TIMESTAMP(DATE(from_unixtime(`endDate`)));
I'm trying to insert rows from one table to the other. In the first table, the datatype of one column is char(5), but the same column has tinyint(4) datatype in the second table. When i run the insert query, it says
Incorrect integer value: '' for column 'x' at row 258
I cannot alter or modify the datatype now as it violates some constraints. Is there a way to use cast or convert char to tinyint?
Thanks.
You probably want something like this:
INSERT INTO newtable
SELECT CASE WHEN x = '' THEN 0 ELSE x END
FROM oldtable
I'm assuming that you want blanks to turn into zeros? If not, then provide the integer value you want blanks to have.
If there are other exceptions, use more alternatives in the CASE expression.