DateTimeOffset.Now in T-SQL - sql

I'm executing a INSERT to a sql 2008 db. How do I specify in T-SQL to insert NOW in a DATETIMEOFFSET column? GETDATE()?

Maybe.
This would give you the local time of the server where SQL is installed.
Do you want to store timezone etc too? If so, SYSDATETIMEOFFSET() may be better

Related

Migrate table from Oracle to SQL Server

Migrate a table from Oracle to SQL Server.
I have used Toad to export (select * from table) into a pipe delimited .txt file so it can be used to be consumed in SQL Server. Now the Oracle table has a DATE column and the output from Toad for that column is (2/26/2016 3.05.10.000000 PM). This format is not being compatible for the datetime column in SQL Server side.
I feel we can convert the date in Oracle to a compatible SQL Server format for easier ingestion.
Please help me understand the conversion both from Oracle to a compatible SQL Server format.
Create Oracle Linked server in SQL Server with ODBC connection. and use that Linked server to play with Oracle and SQL Server tables using SQL Server.
You must understand that DATE datatypes are binary data. Using to_date() on a column that is already a DATE is inappropriate. It forces oracle to perform (behind the scenes) a to_char() on the DATE column in order to produce character data that is the required input to to_date(). Then, when you see (in your text csv file) that it has produced a "date" in some particular format, it is because oracle has then had to run the result of your to_date() back through to_char(), using the default NLS_DATE_FORMAT setting to produce a character string for the text output.
So your solution is this:
First, determine what text format of a date MSSQL wants when it uses this csv file. I don't know what that is, but for the sake of argument, let's say it is 'yyyy-mm-dd'. With that information, construct your SELECT in oracle like this:
select mycol1,
to_char(my_date_col,'yyyy-mm-dd'),
mycol2
from my_table;
That said, I agree with the others, why bother with this cumbersome process in the first place? Or even some other intermediary like SSIS? Why not just create a shared server in MSSQL and query the oracle table directly? Or create a database link in the Oracle DB and, using the oracle transparent gateway as the conduit, INSERT directly into the MSSQL table from Oracle? Either the linked server or the database link will be much faster than any external process.
I would suggest a best way to transfer Oracle table to SQL Serveris by using SSIS package.
You can have a Source as Oracle and your conversion issue can be fixed by Data
Conversion task and your Destination can be SQL Server.

SQL Server datetime2 in OPENQUERY

We're migrating form SQL Server 2005 to 2014 for a pretty large environment. And we've noticed that OPENQUERY behaves differently when interacting with MySQL database when it comes to datetime. Previously, it would translate just fine to DATETIME column. With 2014 (I assume started in 2008 or so), it now converts to DATETIME2 (with maximum precision). This causes problems when comparing to or inserting into DATETIME columns.
Is there a way to specify on a server-level (or specify default) for which type those will translate to? Rewriting all of the queries will be quite an undertaking, and I'd like to avoid this now, if possible (rather rewrite as we edit or introduce new things).
Try to Use VARCHAR datatype while migration of date fields, and it is always easy to Convert/Cast in various types as per need.

Default format DateTime SQL in Oracle, SQL Server and Firebird

I'm doing a manual INSERT where I have to format a DateTime value in C#. The resulting string is put into a a SQL query. I need a cross-platform format that can be hendled by Oracle, SQL Server and Firebird.
Example:
With yyyy-mm-dd you save the date correctly without relying on the language of the database.
I need the format not to uncouple from language but from vendor quirks.

Varchar(max) 2008 to varchar in 2000

I have a query that queries a linked SQL server 2008 database and joins data from a table in that database to a table in SQL server 2000. It was working find until one of the columns in the SQL server 2008 database was changed to varchar(max). I received an error and I fixed it by using CAST(varchar(max) column AS varchar(50)). Now my queries performance is slow compared to how it was before the change. Can you give me some suggestion on how to fix the issue. Thank you for your time.
As the other commenters have pointed out, why was the column changed to varchar(max) if it can be reliably cast to varchar(50)?
Varchar(max) was meant as a replacement for the text datatype and should not just be used casually. A plain varchar can support up to 8000 characters and is recognized by SQL 2000 and 2005.
If you are doing this cast() in the select list, as a join condition, or in the where clause? Distributed queries already perform slow and by adding functions (cast, left, etc) in the where clause or join conditions is only going to make it worse.
Assuming that you cannot change the varchar(max) to a plain varchar, here's an idea
Does the 2005 box have a linked server connection to the SQL 2000 box? If so, can you run the query that way. The 2005 box will be able to compare the varchar(max) to the varchar(50) directly.

Why doesn't SQL Server have a pure date and pure time data type?

I have a few SQL Server tables with a datetime field that really only has to contain dates... and the fact that it's a datetime object violates data integrity, because it's possible for data to be inserted with a time - which isn't really valid for this field. The result is that we have to convert it and strip off the time every single time we use it. We've considered adding triggers to make sure that no data can even get in with a time attached... but it seems to me that this should really be part of the database software, and shouldn't need to be specifically programmed each time.
Why doesn't SQL Server have separate date and time data types? Is this going to change in the near future? Do other database platforms have this functionality?
SQL Server 2008 has DATE and TIME data types.
What version are you using?
SQL Server has a pure date and time data type since version 2008, they also added a bunch of new datetime types, now it also goes all the way back to year 1 instead of 1753