October 16, 2018 at 8:58 am
I have a table with two DateTime fields. When I select them and cast one of them as date, it returns them both as date. For example:
select
orderdate
from tableA
would return results in datetime format. But this:
select
orderdate
,cast(paiddate as date) as paiddate
returns both column as date only. Why would it do that?
October 16, 2018 at 9:09 am
robert.wiglesworth - Tuesday, October 16, 2018 8:58 AMI have a table with two DateTime fields. When I select them and cast one of them as date, it returns them both as date. For example:select
orderdate
from tableAwould return results in datetime format. But this:
select
orderdate
,cast(paiddate as date) as paiddatereturns both column as date only. Why would it do that?
Can you provide some code which allows us to reproduce this? My code behaves as expected:DROP TABLE IF EXISTS #SomeDates;
CREATE TABLE #SomeDates
(
Date1 DATETIME
, Date2 DATETIME
);
INSERT #SomeDates
(
Date1
, Date2
)
VALUES
(
GETDATE(), GETDATE()
);
SELECT
sd.Date1
, sd.Date2
, CAST(sd.Date1 AS DATE)
, CAST(sd.Date2 AS DATE)
FROM #SomeDates sd;
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
October 16, 2018 at 9:34 am
I copy and pasted your code and I get the same results. If I comment out the lines in the select statement that does the casting, the results for Date1 and Date2 show date and time. If I run your code as it is, then all for results show only date, not date and time. WTH?
October 16, 2018 at 9:37 am
Screenshot please? And where are you running this from?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 16, 2018 at 9:46 am
October 16, 2018 at 9:50 am
What client is that?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 16, 2018 at 9:55 am
It is TOAD for SQL Server. I just tried the same code in Management Studio and it worked as expected. So something with TOAD? But the real problem is that it is happening during an SSIS package that is run on a scheduled SQL job. So now it might become a question for the SSIS forum. So weird...
October 16, 2018 at 10:03 am
If it's happening in SSIS, then that's a problem. I can see a client tool weirdness, but SSIS is another matter.
Assuming SSIS is inserting into some table with date and datetime columns, have you checked to see whether Toad and SSMS show different results from that table without the CAST?
eg, does this work as expected?
DROP TABLE IF EXISTS #SomeDates;
CREATE TABLE #SomeDates
(
Date1 DATETIME
, Date2 DATE
);
INSERT #SomeDates
(
Date1
, Date2
)
VALUES
(
GETDATE(), GETDATE()
);
SELECT Date1, Date2 FROM #SomeDates
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 16, 2018 at 10:22 am
That code works as expected in SSMS but again, not in TOAD. I am going to test some things in SSIS to see what I can figure out.
October 16, 2018 at 10:45 am
Then consider the possibility that the problem is not in SSIS, but in Toad's display of the data that SSIS has loaded.
Double-check in SSMS before you go digging into the SSIS packages
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 16, 2018 at 1:59 pm
robert.wiglesworth - Tuesday, October 16, 2018 9:55 AMIt is TOAD for SQL Server. I just tried the same code in Management Studio and it worked as expected. So something with TOAD? But the real problem is that it is happening during an SSIS package that is run on a scheduled SQL job. So now it might become a question for the SSIS forum. So weird...
Heh.... we used to say that they called it "Toad" because it croaks on a regular basis and will give you warts. 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
October 17, 2018 at 9:16 am
OK, so I figured this out. It was in SSIS. I looked at the metadata for the process that was pulling this data and it had dbdate as the data type for that column. It must have carried that from copy and pasting in the query or something. Either way, I edited the data type for that field to dbtimestamp and now everything is working as desired.
Thanks for helping me get through this!
October 17, 2018 at 10:34 am
robert.wiglesworth - Wednesday, October 17, 2018 9:16 AMOK, so I figured this out. It was in SSIS. I looked at the metadata for the process that was pulling this data and it had dbdate as the data type for that column. It must have carried that from copy and pasting in the query or something. Either way, I edited the data type for that field to dbtimestamp and now everything is working as desired.Thanks for helping me get through this!
This does not square up with your earlier post:
I copy and pasted your code and I get the same results. If I comment out the lines in the select statement that does the casting, the results for Date1 and Date2 show date and time. If I run your code as it is, then all for results show only date, not date and time. WTH?
SSIS has nothing to do with this. Please explain.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
October 17, 2018 at 10:43 am
Those problems were only with the TOAD interface. Apparently it has a problems with dates or something when doing select statements. When I ran that code in SSMS it worked fine. But I couldn't figure out why SSIS was not bringing over the time when doing the insert. That's were the metadata settings in SSIS came in.
October 17, 2018 at 10:50 am
robert.wiglesworth - Wednesday, October 17, 2018 10:43 AMThose problems were only with the TOAD interface. Apparently it has a problems with dates or something when doing select statements. When I ran that code in SSMS it worked fine. But I couldn't figure out why SSIS was not bringing over the time when doing the insert. That's were the metadata settings in SSIS came in.
Bit of a coincidence, but OK!
In the north east of England, toad is pronounced 'turd'. I can now see that they are on to something.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply