Assume that I have a database with the following table:
CREATE TABLE Table1 (
start DateTime,
end DateTime,
activityId Guid,
level string,
INDEX Table1 CLUSTERED(start ASC) PARTITIONED BY HASH(level)
);
And I'd like to get the duration of each activity, so I try to run the following code:
#result = SELECT activityId, level, (end - start) AS duration
FROM Table;
The column duration is being outputted in the correct "TimeSpan" format (e.g. "00:00:00.0123").
However, if I try to perform calculations on that columns, such as AVG/MAX/etc, I'm getting an error saying that TimeSpan cannot be used as a column type.
TimeSpan is not a supported built-in type. TimeSpan is basically a long, so you can convert it into a long and then perform your operations and at the end, cast it back.
In the future we will be adding support for User-defined types. In that case you can either wrap it into a UDT or implement the serialization interface that you will need to provide.
If you would like us to add TimeSpan as a built-in type, please file a request at http://aka.ms/adlfeedback.
Related
I am using Microsoft SQL Server Management Studio to manipulate Public transport system database.
I have a table in database named 'dbo.tblStop_times', two columns of which should be of data type time. At the moment, they are both nvarchar and they have data stored in a pattern like this - "07:39:00" (without quotes)
I had same question with date columns as well, but found a solution for that on stackoverflow just few hours back.
Below works fine for conversion of nvarchar column to date column.
ALTER TABLE tblCalendar ALTER COLUMN [start_date] DATE NOT NULL;
I am not sure if what I want is achievable or not, because the above mention conversion works just fine, I assume it might be possible.
What I have atm is - nvarchar(8), what I want it to be is sql time data type, something like hh:mm:ss [and if possible - without trailing nnnnnn - nanoseconds component]
You should be able to do:
ALTER TABLE tblStop_times ALTER COLUMN start_time TIME NOT NULL;
Here is a rextester.
EDIT:
If you don't have valid time values, then you have a problem. You should first look for the values:
select col
from tblStop_times
where try_convert(time, col) is null;
This will show you the values that cannot be converted. If you like, you can NULL them out so the alter will work:
update tblStop_times
set col = NULL
where try_convert(time, col) is null;
I'm creating an incremental load which would be pulling data from ORACLE to SQL Server. The incremental load will be based off a MODIFIED_DATE column.
I have created a result set variable that stores the MAX modified_date from the destination table. So the engine will only check the rows of the MODIFIED_DATES that are greater than the variable and perform a lookup to see if the row needs to be added, updated or deleted.
So I have my MAX MODIFIED DATE RESULT SET and I also have created another variable that will house the SOURCE QUERY which will be have a WHERE clause that see if the MODIFIED_DATE column is greater than the MAX MODIFIED_DATE variable.
Example:
Select column_name,column_name
From table
Where modified_date > '"+ #[User::LastModifiedDate]+ "'"
It is throwing me an error off:
The data types "DT_WSTR" and "DT_DATE" are incompatible for binary operator "+". The operand types could not be implicitly cast into compatible types for the operation. To perform this operation, one or both operands need to be explicitly cast with a cast operator.
Now, I have done a ton of searching but I cant seem to find a way to do this. The only solution that I found online is to ADD A (DT_WSTR, 25) in front of the variable which causes the variable expression to evaluate and this is the only way I can get the variable expression to evaluate.
Example:
(DT_WSTR, 25) #[User::LastModifiedDate]+ "'"
When I run it it is telling me it is NOT A VALID MONTH
The MODIFIED_DATE column in the DESTINATION table is in SQL Server and it has a DataTime as the date type which reads like this:
2008-06-10 22:22:25.000
YYYY-MM-DD
The MODIFIED_DATE column in the SOURCE table in oracle reads like:
6/10/2008 10:22:25 PM
MM/DD/YY HH:MM:SS
How would I be able to resolve this? Also what do you think is the best way to perform an incremental load based the MODIFIED_DATE column? Is my way one of the more efficient ways or is there another route I can take?
You need to make your SSIS component call the following statement:
Select column_name,column_name
From table
Where modified_date > to_date('whateverformat','"+ (DT_WSTR,25)#[User::LastModifiedDate]+ "')"
The problem is you are tangling the strings versus dates. Your lastmodifieddate must be a string for the expression builder to function, but i suspect Oracle is expecting modified_date to be a date, so just use the to_date function
I am working on pulling some data in from a Rest-api into my MSSQL database. The issue I am having is the timestamp that I am being given from the api does not appear to be formatted correctly to just insert '2013-09-16T07:00:00+0000'.
example insert:
INSERT INTO [page_fan_adds_unique]([period], [title], [description], [value], [end_time])
VALUES ('day', 'Daily New Likes', 'Daily: The number of new people who have liked your Page (Unique Users)','0', '2013-09-16T07:00:00+0000')
I know changing the format to 2013-09-16T07:00:00+00:00 works but I didn't want to have to manipulate the data before the insert.
You cannot do this without formatting.
From the MSDN about TIMESTAMP:-
Is a data type that exposes automatically generated, unique binary
numbers within a database. timestamp is generally used as a mechanism
for version-stamping table rows. The storage size is 8 bytes. The
timestamp data type is just an incrementing number and does not
preserve a date or a time. To record a date or time, use a datetime
data type.
You may use datetime data instead.
This is what I did. from my PHP code I inserted that date [end_time] = CAST(STUFF('".."',23,3,':00')AS DATETIME2)"
Now when I write my "select" statements I just cast(end_time as datetime) I find that this is a lot easier than trying to cast a string into a datetime in sql.
I need help with a SQL convert statement. I have NetQuanity (masterTable) which is a varchar(15) and I have another table with Purchase price (PO TABLE) which is money. When I try to multiply them in a SQL view is gives me the error:
If your field is a VARCHAR, you'll need to CAST to the appropriate data type prior to your operation. e.g.
CAST(myVarCharField as INT) * myIntField
Be forewarned however, if you attempt to CAST this field to a numeric data type and it's not numeric, you'll be in the same boat.
I would recommend using CAST over CONVERT in your example, for the following reason defined in this SO post:
Related: T-SQL Cast versus Convert
Maybe try using the CONVERT function? CONVERT(money,NetQuantity).
First of all you have a data definition problem.
The first thing is to eliminate any non-numeric entries in the master table.
SELECT whatever FROM masterTable WHERE ISNUMERIC(NetQuanity)=1
The next step is to include this as a sub-query in the calculation.
In this query use CONVERT or CAST to convert the valid quanities to integer.
i.e.
CONVERT(INT, NetQuantity)
The question
Is it possible to ask SSIS to cast a value and return NULL in case the cast is not allowed instead of throwing an error ?
My environment
I'm using Visual Studio 2005 and Sql Server 2005 on Windows Server 2003.
The general context
Just in case you're curious, here is my use case. I have to store data coming from somewhere in a generic table (key/value structure with history) witch contains some sort of value that can be strings, numbers or dates. The structure is something like this :
table Values {
Id int,
Date datetime, -- for history
Key nvarchar(50) not null,
Value nvarchar(50),
DateValue datetime,
NumberValue numeric(19,9)
}
I want to put the raw value in the Value column and try to put the same value
in the DateValue column when i'm able to cast it to Datetime
in the NumberValue column when i'm able to cast it to a number
Those two typed columns would make all sort of aggregation and manipulation much easier and faster later.
That's it, now you know why i'm asking this strange question.
============
Thanks in advance for your help.
You could also try a Derived Column component and test the value of the potential date/number field or simply cast it and redirect any errors as being the NULL values for these two fields.
(1) If you just simply cast the field every time with a statement like this in the Derived Column component: (DT_DATE)[MYPOTENTIALDATE] - you can redirect the rows that fail this cast and manipulate the data from there.
OR
(2) You can do something like this in the Derived Column component: ISNULL([MYPOTENTIALDATE]) ? '2099-01-01' : (DT_DATE)[MYPOTENTIALDATE]. I generally send through '2099-01-01' when a date is NULL rather than messing with NULL (works better with Cubes, etc).
Of course (2) won't work if the [MYPOTENTIALDATE] field comes through as other things other than a DATETIME or NULL, i.e., sometimes it is a word like "hello".
Those are the options I would explore, good luck!
In dealing with this same sort of thing I found the error handling in SSIS was not specific enough. My approach has been to actually create an errors table, and query a source table where the data is stored as varchar, and log errors to the error table with something like the below. I have one of the below statements for each column, because it was important for me to know which column failed. Then after I log all errors, I do a INSERT where I select those records in SomeInfo that do not have an errors. In your case you could do more advanced things based on the ColumnName in the errors table to insert default values.
INSERT INTO SomeInfoErrors
([SomeInfoId]
,[ColumnName]
,[Message]
,FailedValue)
SELECT
SomeInfoId,
'PeriodStartDate',
'PeriodStartDate must be in the format MM/DD/YYYY',
PeriodStartDate
FROM
SomeInfo
WHERE
ISDATE(PeriodStartDate) = 0 AND [PeriodStartDate] IS NOT NULL;
Tru using a conditional split and have the records where the data is a date go along one path and the other go along a different path where they are updated to nullbefore being inserted.