valid date format for sybase, oracle and sql server - sql-server

I am working on a replication project where data is replicated on oracle as well as sql server from sybase database.
Basically one of the tables in sybase get populated with the data to be replicated
Sybase column contains data like -
{"a":"b","c":"d","created_date":"'Feb 13 2018 1:33AM'"}
So basically when we are forming query to replicate on sql server and oracle we are using date string 'Feb 13 2018 1:33AM' to convert into date for oracle and sql server.
This date string works fine with the sql server but failed with the error like -
ORA-01858 A NON NUMERIC CHARACTER WAS FOUND WHERE A NUMERIC WAS EXPECTED
So which date format should I use in Sybase so that it will work for both oracle and sql server to replicate.

Oracle will implicitly try to convert strings to dates using the TO_DATE( date_string, format_model, nls_params ) function. Without a format model, Oracle will use the default date format which is a session parameter (so is a per-user setting) stored in the NLS_SESSION_PARAMETERS table and you can find the default value using:
SELECT value
FROM NLS_SESSION_PARAMETERS
WHERE parameter = 'NLS_DATE_FORMAT'
This means the conversion will implicitly be:
TO_DATE(
'Feb 13 2018 1:33AM',
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT')
)
and if the format model does not match your string then you will get an exception.
You can either set the output format to match Oracle's default or you could alter the default date format in the current Oracle session to match your current data using:
ALTER SESSION SET NLS_DATE_FORMAT = 'Mon DD YYYY HH12:MIAM';
You can also create a logon trigger to change the NLS settings when the user connects to the database and starts a session.
The other alternative is that instead of trying to use a string you use a timestamp literal, since you can specify a time component (which you can't with a date literal), and then let Oracle cast it back to a date:
TIMESTAMP '2018-02-13 01:33:00'
or you could explicitly call TO_DATE in your replication query for Oracle and specify the date format:
TO_DATE( 'Feb 13 2018 1:33AM', 'Mon DD YYYY HH12:MIAM' )

Related

Error in SQL Server Agent: "the conversation of a varchar data type to a datetime data type resulted in an out-of-range.""

I created a small script that works in SQL Server Management Studio, then I created a SQL Server Agent job with that same script; but after I run it, I get an error:
The conversation of a varchar data type to a datetime data type resulted in an out-of-range
I fixed the problem by change the format of date to ISO 8601, but I don't release how my first script works on SSMS and not in SQL Server Agent.
First script:
declare #teste datetime
set #teste = '31/12/2099 00:00:00'
select #teste
Fix error:
declare #teste datetime
set #teste = '20991231 00:00:00'
select #teste
This is one reason why using unambiguous formats are so important when using date(time) datatypes. The only formats that an unambgious in SQL Server, regardless of language and datatype are the formats yyyyMMdd and yyyy-MM-ddThh:mm:ss.
For the date '31/12/2099 00:00:00' and the language your Login is using it appears that SQL Server is interpreting the value as the 12th day, of the 31st month, of the year 2099. There aren't 31 months in the year, and hence the error. (DB<>Fiddle). It's worth noting that date (and the other "new" datetime data types) behave differently and also can unambiguously understand the format yyyy-MM-dd; notice in this DB<>fiddle the difference in the values for the datetime values simply due to the language setting.
As you can see, the solution is to use a unambiguous format. So, as you're using a date and time, I would suggest the string '2099-12-31T00:00:00'.

SQL Server 2016 - how to date setting

I'm using SQL Server 2016 (free version). The date camp is set to "yyyymmdd" but I would like that the data can be store in the table like "mmddyyyy".
What can I do ?
or this using format and cast (to make sure that it's of date data type)
select format(cast(#date as date),'MMddyyyy')
-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse,
SET DATEFORMAT { format | #format_var }
Arguments
format | #format_var
Is the order of the date parts. Valid parameters are mdy, dmy, ymd, ydm, myd, and dym. Can be either Unicode or double-byte character sets (DBCS) converted to Unicode. The U.S. English default is mdy.

how do I get date time including milliseconds in VBA

I have a VBA macro ( excel vba ) where I have some code querying a SQL Server database field. The field is of type datetime
The field has values including milliseconds.
if I query the field with ADO and the Recordset will give me teh field value formatted and without the milliseconds bit
for example the actual field value when queries in SQL management studio
is
2015-12-14 10:19:48.077
but the recordset value is
14/12/2015 10:19:48 AM
How can I get the actual value ?
Try casting as string. Like This in SQL Server.
select CONVERT(nvarchar(50), GETDATE(), 113) as MyDateAsStringWithMS
Casting a date as string is useful in scenarios where your are converting between SQL and ORACLE and MySQL and ODBC level libraries like in Java and VBA and COM.
I chose 113 on purpose. I have had a lot of success using that format when I want milliseconds.

Format date of filters and results on SQL Server

I'm in a trouble with the format of dates on my queries.
In SQL Server when I execute the following query:
select *
from users
where register_date >= '2015-03-17'
It throws me a cast conversion error.
But if I do execute the following query:
select *
from users
where register_date >= '2015-17-03'
It returns me the correct data, BUT when I see the the register_date column, it gives me the dates as 'YYYY-MM-DD' format...so it's a little confusing...
How can I configure SQL Server to work always with "YYYY-MM-DD" format, on filters and results?
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.
The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.
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 use 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
So in your concrete case - what if you run this query:
select *
from users
where register_date >= '20150317'
Do you get the expected results?
Your Date format in SQL SERVER is 'YYYY-DD-MM' that's why you are getting cast conversion error. For searching you must give the date with this exact format.
But In case of getting result you can use SQL DATE FORMAT and retrieve date in any format e.g
SELECT DATE_FORMAT(users.register_date ,'%Y-%d-%m') AS date FROM users
Result will be : 2015-17-03

Inserting CDate to SQL Server 2012 Flip Day and Month

I have a problem inserting a date from a VB.net Program to a SqlServer2012 instance.
First here is how i generate the data (Vb.net)
ExitTime = CDate("1.1.1970 00:00:00").AddSeconds(currentField).ToLocalTime
We add this value to a stored procedure (Vb.net)
With comsql5.Parameters.AddWithValue("#ExitTime", ExitTime)
In the Sql Server stored procedure
#ExitTime datetime, [...]
[...]
Insert into [table] ([ExitTime]) VALUES (#ExitTime)
Here is the output of the exit time in the vb.net
Exit Time : 08/07/2014 2:06:31 PM
Here is the same row in the Sql server database
2014-08-07 14:06:31.000
What I would like to see in the database is 2014-07-08 14:06:31.00
Because another part in the program does a check on the field but as a String... and it does not match because it flip the month and day
EDIT: TO be clear, I can't change the other part that does the comparison as a string. I know this is a poor way to compare datetime.
Thank for your time
Have you tried using the Convert function?
SELECT CONVERT (VARCHAR, getdate(), 121);
Check this links for more information MSDN - CAST and CONVERT and SQL Server Datetime Format

Resources