I'm using SQL Server 2012 Enterprise. I have a requirement where I've to store data with DATETIME2 datatype in SSIS Variable. Unfortunately, SSIS variables don't have that data type.
If I'm storing it into datetime data type, I'm losing information. Can anyone help in giving workaround?
PS: My source system is SQL Server 2012 as well and I'm reading the data from column with datetime2 datatype.
SSIS, at least currently, has a "known" shortfall in that the variable value type, DateTime only has a precision to the second; effectively the same as a datetime2(0). If you therefore need to store anything more accurate that a second, such as if you are using datetime and the 1/300 of a second is important or if you are using datetime2 with a precision of 1 or more, the value type DateTime, will not serve your goal.
A couple of different options are therefore to store the value of as a String or numerical value. This does, however, come with it's own problems; most and foremost that neither of these datatypes are date and time datatypes.
It therefore depends what your goal is. I would most likely make use of a String datatype and ensure it has the ISO format ('yyyy-MM-ddThh:mm:ss.nnnnnnn'). If you're then using something like a T-SQL Task you can pass your variable as normal to the task and the data engine will interpret the literal string as a datetime2(7) (or whichever literal precision you used).
Related
I have a column of type timestamp which updates every time record updated.
We have some specific logic for syncronizing data and I'd like to store timestamp's value in other table so I can compare to source later. Can't make new column of type timestamp as it's auto-updated.
Which SQL type should I use in TableB for maximum performance and quicker CASTs on comparisons?
Taken from the docs
timestamp is the synonym for the rowversion data type and is subject
to the behavior of data type synonyms. In DDL statements, use
rowversion instead of timestamp wherever possible. For more
information, see Data Type Synonyms (Transact-SQL).
and
The timestamp syntax is deprecated.
and
Is a data type that exposes automatically generated, unique binary
numbers within a database. rowversion is generally used as a mechanism
for version-stamping table rows. The storage size is 8 bytes.
So putting this together: Think of TIMESTAMP (which should be ROWVERSION) as a meaningless 8-byte binary. If you really want to do something with it, you can store it as 8-byte binary or you can convert it to a type with a length of 8 bytes. In this case I'd suggest BIGINT.
First, understand that timestamp is NOT a value of type date and time, but rather a unique identifier based on this type of data.
I suggest you review the documentation Date and Time Data Types and Functions (Transact-SQL) and choose the type of data that best suits your needs. This could be DATETIME2, DATETIMEOFFSET, DATETIME
I have recently starter working with sql server instead of mysql.
I have a time column which I've noticed stores time as such: hh:mm:ss:nnnnnnn
Thing is I only want to to store hh:mm:ss, and I can't seem to figure out how to do that.
I do apologize if this is really obvious, I have had a long day and my searches did not return anything that helped.
time has a precision parameter which is "fractional second precision" so if you need hh:mm:ss you have to use time(0) as datatype
use the Time datatype which is available since 2008 onwards
If on SQL 2008, use the time datatype, and just store the hh:mm:ss; the nanoseconds will be 0. Just display the value back in the desired format in your presentation layer.
If on SQL 2005 (or older), you'll need to store the data as a datetime, and input the date version as some constant (usually 1900-01-01). Again, format the data at the presentation layer, not at the database.
Here is may code
Create table dbo.EXA
(
Name VARCHAR,
ID INT
)
How many character will hold data type VARCHAR as I am not defining the size?
or defining
NAME VARCHAR
it self wrong?
You will get EXACTLY ONE CHARACTER - which is typically not what you want. This is in the case where you define a SQL Server variable, a parameter on a stored procedure, or a table column.
If you don't specify any length in VARCHAR in the context of a conversion using CONVERT or CAST, then the default is 30 characters.
My recommendation would be to ALWAYS explicitly define a length - then it's clear (to you, to T-SQL, to a poor guy who needs to maintain your code in a year) what you wanted/needed
I connected to SQL Server 2005, ran the above and checked the table after completion and it set it automatically to one character.
varchar is equivalent to varchar(1)
I tried this code check this out
Demo
I am attempting to use VB.NET EntityFramework to add a new record where on of the fields is of the type of DateTime.
Do the two types of DateTime match up to allow direct saving without any cast or formatting?
I.E Should the following work (where table is WidgetResult and I attempt to add a new record)
Private Sub AddNewWidgetResultRecord ()
Dim newWidgetResult As New WidgetResult()
newWidgetResult.SomeVarChar = "Some widget string"
newWidgetResult.SomeDateTime = DateTime.Now ' Would this line be OK?'
myContext.AddToWidgetResult(newWidgetResult)
myContext.SaveChanges()
End Sub
I have checked a lot of other posts on this but they all seem to concern INSERT queries rather than using the EF provided methods.
Yes, they formats are compatible. BUT, one thing I would be cautious of, if you are trying to compare the SQL Server DateTime (GetDate()) to the VB DateTime.Now. You CANNOT guarantee that those will be equal.
If possible, you want SQL Server to be using datetime2 rather than datetime (but it's only available from 2008 onwards).
datetime itself has a more limited range (only from 1753 onwards, not from the year 0) and precision (milliseconds values are rounded to the nearest value ending with 0, 3 or 7)
datetime2 (SQL Server) and DateTime (.NET) are compatible.
How, precisely, you ensure that the database side is using datetime2 may depend on exactly what form of EF you're using.
The other answers are mostly correct in that the types are compatible, but they are not exactly equivalent.
The biggest difference is what Damien described about how datetime has limited range and precision, while datetime2 has the full range of a DateTime and variable precision.
And Ganders is correct that you can't guarantee DateTime.Now == getdate(). Mostly this is because of the clock ticking away in between your calls, but also it's possible that DateTime.Now is called from a web server and getdate() is called from a SQL Server and they are on two different computers. Their clocks might not be perfectly synchronized, or they might have different time zone settings.
But the other point that has not been discussed is that DateTime has its very important .Kind property, which is one of three DateTimeKind values. Either Utc, Local, or Unspecified. A SQL Server datetime or datetime2 does not have this concept.
So if you have a Utc or Local kind of DateTime, when you save and then retrieve it, you will find that it is now Unspecified. In other words - the kind does not survive the round-trip.
By comparison, the related .NET DateTimeOffset type will fully round-trip with a SQL Server datetimeoffset type.
You can read more about this (and other concerns) in my blog post The Case Against DateTime.Now.
We have a utility written in C which reads columns extracted from a database using a stored procedure and outputs a csv file. Simple huh. However when reading a smallint column, it crashes out and not being the greatest C programmer on the planet, I can't nail it. As a workaround can you change the data type in a stored procedure e.g. could the C program "see" the column as a varchar rather than a smallint at runtime?
This is only a monthly process so the impact of doing the type conversion is not an issue.
Yes, it can. You can do so using either the CAST or CONVERT operator. For instance:
CONVERT(varchar, MyIntColumn) AS MyIntColumn
That will ensure that when the column goes to the client, it goes as a varchar string.