Querying Oracle through SQL Server linked server and date values - sql-server

I am trying to reach some data in a legacy Oracle DB that is attached to a SQL Server as a "linked server". I have an Oracle table and one of the columns is of Date type. Values in this column can be NULL.
In the query I am building I hope to return either just the "date" portion of the column value or an empty string if the value is NULL.
I am currently trying:
CASE
WHEN ACCOUNT.DATE_REVOKED IS NULL
THEN ''
ELSE CONVERT(DATE, ACCOUNT.DATE_REVOKED)
END
This works for values with actual dates. For NULL values what is returned as "1900/01/01". If I do not use the CASE and just return the result of
CONVERT(DATE, ACCOUNT.DATE_REVOKED)
I will get the date portion or the text "NULL", at least in SQL Server Management Studio.
What am I missing?

as the comments have suggested, you cannot use empty string in date column, therefore, why not use this instead:
CASE
WHEN ACCOUNT.DATE_REVOKED IS NULL
THEN NULL
ELSE CONVERT(DATE, ACCOUNT.DATE_REVOKED)
END

Related

SQL Server Determining Hard Coded Date as Larger When It's Not?

An old employee left a massive query behind that I've been debugging and it appears that the issue has come down to SQL Server itself determining a comparison differently than what I would have expected.
I have a table with a column col1 containing the value 20191215 as a datetime.
The part in question is similar to the following:
select case when col1 > '01/01/2020' then 1 else 0 end
This statement is returning 1, suggesting that '12/15/2019' is larger than '01/01/2020'.
I do not need assistance correcting the query, as I have already made changes to do so other than using the comparison the previous employee was using, I am simply curious as to why SQL Server would evaluate this as I have described.
I understand that this is not the typically way SQL Server would store dates as well, would the issue simply be the formatting of the dates?
Current SQL Server version is: SQL Server 2014 SP3 CU3.
SQL Fiddle link that shows the same results
Please note that the link does not contain an exact replica of my case
Edit: Included additional info relevant to actual query.
It is a string comparison not a date comparison:
select case when '12/15/2019' > '01/01/2020' then 1 else 0 end
vs
select case when CAST('12/15/2019' AS DATE) > CAST('01/01/2020' AS DATE) then 1 else 0 end
db<>fiddle demo
I am simply curious as to why SQL Server would evaluate this as I have described.
'12/15/2019' it is a string literal, SQL Server does not know you want to treat a date unless you explicitly express your intention.
I have a table with a column col1 containing the value 20191216
If you are comparing with a column then the data type of column matters and data type precedence rules

Microsoft Access 2016 Query Returns 1/1/1800 for Null Dates in Linked Table

I have a Access 2016 database with linked tables. They're linked to an Azure SQL database. When I include the table in an Access query, null dates appear as 1/1/1800. If I just open the table in Access null dates are blank. Here is the query:
SELECT dbo_WTSI_Jobs.Id, dbo_WTSI_Jobs.JobNo, dbo_WTSI_Jobs.CreatedDate, dbo_WTSI_Jobs.CompletedDate
FROM dbo_WTSI_Jobs
WHERE (((dbo_WTSI_Jobs.CreatedDate)>#1/1/2017#));
This query returns dbo_WTSI_Jobs.CompletedDate as 1/1/1800 when it is null. If I omit the WHERE clause, null dbo_WTSI_Jobs.CompletedDate values are empty strings.
How do I prevent the query from returning 1/1/1800 ?
Change the data type of the field to datetime.
The following solved my problem: CompletedDateCalc: IIf([CompletedDate]=#1/1/1800#,"",[CompletedDate]).

how to convert julian date(csv file) to normal date in sql server

We have tried concatenate data in excel so as to make the insert query, but the date col got converted from 28/04/2017 to '42853'(text format) and when we inserted the data onto sql server it comes in text format.
Name Date
Neha 28/04/2017
but wh
Adding the number from excel as days to the date 1899-12-30 will return a proper date.
select dateadd(day,42853,'18991230')
returns: 2017-04-28
In SQL Server 2012+ you can use try_cast() to convert your text value to an integer.
select
...
, dateadd(day,try_cast(col as int),'18991230')
from t
Prior to that, you can use patindex() to in a case expression to confirm that the column is only numbers before attempting to cast() if there is a chance that some columns in the text column are not numbers.
select
...
, case when patindex('[0-9][0-9][0-9][0-9][0-9]',col) = 1
then dateadd(day,cast(col as int),'18991230')
else null
end
from t

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

Convert oracle date string to SQL Server datetime

In a SQL Server 2000 DB, I have a table which holds string representations of Oracle DB dates. They are formatted like "16-MAY-12". I need to convert these to datetime. I can not seem to find a conversion style number that matches, nor can I find a function that will allow me to specify the input format. Any ideas?
This seems to work for me:
SELECT CONVERT(DATETIME, '16-MAY-12');
You can also try using TO_CHAR() to convert the Oracle values to a more SQL Server-friendly format (best is YYYYMMDD) before pulling them out of the darker side.
Follow Aaron's advice and cast to string on the Oracle side and then did a check/recast on the MS SQL side. See example below:
;WITH SOURCE AS (
SELECT * FROM openquery(lnk,
'SELECT
TO_CHAR(OFFDATE , ''YYYY-MM-DD HH24:MI:SS'') AS OFFDATE,
FROM
ORACLE_SOURCE')),
SOURCE_TRANSFORM AS
(
SELECT
CASE
WHEN ISDATE(OFFDATE) = 1 THEN CAST(OFFDATE AS DATETIME)
ELSE NULL END AS OFFDATE
FROM
SOURCE
)
SELECT * FROM SOURCE_TRANSFORM

Resources