Progress to SQL Server Datetime Conversion with Pro2 - sql-server

We are using Pro2 to sync data from Progress to SQL Server. However, some datetime (or datetime offsets) are being converted into int32 and we cannot figure out the conversion. We have asked Progress but no reply. Curious if anyone is familiar with this.
I have found various articles regarding Oracle, Epoch, Julian, etc. but no definitive answers. Reference: Working with MS SQL Server and ABL datetime data types (progress.com)
For time columns, it is fairly straightforward. This code works:
select cast(dateadd(s, 86055, '00:00:00') as time(0)) -- returns 23:54:15
Here are some values with what should be the approximate date:
966188916 -- should be approx. 4/12/2010
1091012202 -- should be approx. 7/28/2014
Anyone know how to parse these?

Related

Get date in date format from SQL Server

I have a table in SQL Server with a date column set to default date, however, when I fetch the data from the server using VBA I get the date column in string format. Is there any way I get the date as in date format?
Can you you advise how you are bringing in the data? EG from inbuilt connectors or via a recordset via VBA objects?
A quick hack which I wouldn't advise without really testing would be this :
Select DateDiff(DD, '1899-12-30', date_column) AS [column] from TABLE_x
What is this doing?
In Excel dates start 01-01-1900. When you change a date to 'General' you find it's actually a number with a format. The above date difference will give you the excel number version of the Date.
EG to get today 29/11/2019 which will be 43798 when cell format is 'General'.
Select DateDiff(DD, '1899-12-30', '2019-11-29') AS [column]
More info here
Non MS site explaining the situation
MS Office Info touching on the subject
Please note this basing your question on a SQL formatted date.

SQL DateTime Inserted Incorrectly From VB.NET

Have a table I'm logging information from a .NET program into.
The VB.NET app explicity dictates the format of the DATETIME string like below
responsedt = Date.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
I then pass this into an INSERT statement that updates my table, however even though the entire setup of the SQL Server is en-GB (British English) the DateTime has gone in the following format:
2019-09-05 19:09:34.823
This was done yesterday so actually should be
2019-05-09 19:09:34.823
The day and month should be switched around, I have tried performing an update on the table post process to get it to update using the following code
FORMAT (xa.daterequested, 'yyyy-dd-MM HH:MM:ss.fff', 'en-gb')
How while this works in a SELECT statement it doesn't seem to work when I do the UPDATE statement.
It is not ideal to have to update all the records dates after the initial INSERT so a solution to either the .NET side of the issue or the SQL would be appreciated as its pickling my head.
You have 2 options to prevent the error from happening again:
Keep dates as date/time data types instead of converting them to strings.
Use formats that are not language or settings dependent. In SQL Server these would be YYYYMMDD hh:mm:ss.msss OR YYYY-MM-DDThh:mm:ss.mss (notice the T between date and time)
To correct the dates already inserted you could use the format codes in the CONVERT function.
UPDATE t SET
daterequested = STUFF( STUFF( StringDate, 5, 0, SUBSTRING(StringDate,7,2)), 9, 2, '')
FROM YourTable t
CROSS APPLY( SELECT CONVERT( varchar(25), '20190905 19:09:34.823', 121) AS StringDate) AS x;

Non deterministic data type in SQL Server

I was creating a table in my SQL Server database when a specific attribute Deterministic of the database columns caught my attention. Please see the screenshot below:
I am already aware of Deterministic and Non-Deterministic SQL Server functions but I want to know that does it apply even to data data types in SQL Server in any way?
The reason why I'm asking is that I literally scanned through all the data types available in SQl Server v2008 and v2012 but the value of Deterministic field showed Yes for all of them. It didn't show No for any single data type.
So my question is that is it a proper attribute of any data type in SQL Server which still affects the way values are stored in the column or it is just a legacy from the past, may be from SQL Server 2000 or SQL Server 2005 where there used to be some data types which were non-deterministic in nature. Any information will be very useful to understand this characteristic of data types in SQL Server. Do we have any data type in SQL Server as of today which is non-deterministic in nature?
Since I didn't see No for any of the data types I got more got confused. I also googled a lot but every search takes me to Deterministic and Non-Deterministic SQL Server functions like the one below and nobody talks about Non-deterministic characteristic relating to SQL Server data types.
https://technet.microsoft.com/en-us/library/ms178091(v=sql.110).aspx
If you read this MSDN documentation carefully, you'll find:
IsDeterministic - Column is deterministic. This property applies only
to computed columns and view columns.
It applies to columns (computed, view) derived from another columns with non-deterministic functions involved.
Examples:
CREATE TABLE Deterministic
(
ID int,
Calculated AS SYSDATETIME()
)
SELECT COLUMNPROPERTY(OBJECT_ID('Deterministic'), 'Calculated', 'IsDeterministic') IsDeterministic
--Returns 0
If you create view on this table as follows and execute following query
CREATE VIEW vDeterministic AS
SELECT ID, Calculated, DATEADD(D, 1, Calculated) Tomorrow
FROM Deterministic
GO
SELECT 'Calculated' ColumnName,
COLUMNPROPERTY(OBJECT_ID('vDeterministic'), 'Calculated', 'IsDeterministic') IsDeterministic
UNION ALL
SELECT 'Tomorrow',
COLUMNPROPERTY(OBJECT_ID('vDeterministic'), 'Tomorrow', 'IsDeterministic')
You also receive non deterministic columns
ColumnName IsDeterministic
---------- ---------------
Calculated 0
Tomorrow 0

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.

what is the best way to insert only datepart of system date into SQL Server

I'm currently using
Convert(varchar, Getdate(), 101)
to insert only date part of system date into one of my sql server database tables.
my question is: is it the right way to do that or is there any other better method to do it?
I don't understand why you're converting the GETDATE() output (which is DATETIME already) to a VARCHAR and then SQL Server would convert it back to DATETIME upon inserting it again.
Just use:
INSERT INTO dbo.YourTable(SomeDateTimeColumn)
VALUES(GETDATE())
If you're doing that conversion to get rid of the time portion of the DATETIME, you should better:
use the DATE datatype (available in SQL Server 2008 and newer) to store only the DATE (no time)
if you're using SQL Server 2005 or earlier, use this conversion instead - should be much more efficient than two conversions!
INSERT INTO dbo.YourTable(SomeDateTimeColumn)
VALUES(DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
Update: did some performance testing, and in this particular case, it seems the amount of work that SQL Server needs to do is really the same - regardless of whether you're using the convert to varchar stripping the time and back to datetime approach that you already have, or whether you're using my get the number of days since date 0 approach. Doesn't seem to make a difference in the end.
The BEST solution however would still be: if you only need the date anyway - use a column of type DATE (in SQL Server 2008 and newer) and save yourself any conversions or manipulations of the GETDATE() output altogether.

Resources