Default format DateTime SQL in Oracle, SQL Server and Firebird - sql

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.

Related

Does Format Function Work In SQL Server 2012?

I want to use the format function to format a phone number in an SQl 2012 database but i keep on saying that format is not a recognized build in function? what do you think is wrong?

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.

Dateformat in .net Framework

In my application rarely I am getting an error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
I am aware this error is from SQL Server, that query I have formed from C# code.
Convert.ToDateTime(userDate).AddMonths(1)
Here both application server and database server date formats involved, hence I had doubt with date format difference between two servers.
Basically I want to know which date format will be considered for .net and SQL.
I am using .net framework 3.5 and SQL Server 2012.
Thanks in Advance.
Hari
There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.
The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.
The ISO-8601 format is supported by SQL Server comes in two flavors:
YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!
or:
YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.
This is valid for SQL Server 2000 and newer.
If you're using SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.
Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.
The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible

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.

DateTimeOffset.Now in T-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