SELECT statement from MS SQL returns dates with loss of 2-4 days











up vote
1
down vote

favorite












UPD: Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!



I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.



The code looks like:



ResultSet rs = preparedStatement.executeQuery();
while(rs.next()){
java.sql.Date date = rs.getDate("DATE");
}


Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".



I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!










share|improve this question




















  • 3




    Could you add example dates from your DB for both cases, along with the result on java side ?
    – Arnaud
    Nov 9 at 8:35






  • 1




    Just check the timezone for the both DB
    – SaviNuclear
    Nov 9 at 8:35










  • The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try LocalDate date = rs.getObject("DATE", LocalDate.class) instead? (The java.sql.Date class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
    – Ole V.V.
    Nov 9 at 9:03








  • 1




    Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
    – ReemuS
    Nov 9 at 9:10






  • 1




    Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
    – O. Jones
    Nov 9 at 11:47















up vote
1
down vote

favorite












UPD: Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!



I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.



The code looks like:



ResultSet rs = preparedStatement.executeQuery();
while(rs.next()){
java.sql.Date date = rs.getDate("DATE");
}


Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".



I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!










share|improve this question




















  • 3




    Could you add example dates from your DB for both cases, along with the result on java side ?
    – Arnaud
    Nov 9 at 8:35






  • 1




    Just check the timezone for the both DB
    – SaviNuclear
    Nov 9 at 8:35










  • The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try LocalDate date = rs.getObject("DATE", LocalDate.class) instead? (The java.sql.Date class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
    – Ole V.V.
    Nov 9 at 9:03








  • 1




    Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
    – ReemuS
    Nov 9 at 9:10






  • 1




    Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
    – O. Jones
    Nov 9 at 11:47













up vote
1
down vote

favorite









up vote
1
down vote

favorite











UPD: Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!



I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.



The code looks like:



ResultSet rs = preparedStatement.executeQuery();
while(rs.next()){
java.sql.Date date = rs.getDate("DATE");
}


Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".



I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!










share|improve this question















UPD: Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!



I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.



The code looks like:



ResultSet rs = preparedStatement.executeQuery();
while(rs.next()){
java.sql.Date date = rs.getDate("DATE");
}


Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".



I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!







java sql-server date datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 10:32

























asked Nov 9 at 8:32









ReemuS

193




193








  • 3




    Could you add example dates from your DB for both cases, along with the result on java side ?
    – Arnaud
    Nov 9 at 8:35






  • 1




    Just check the timezone for the both DB
    – SaviNuclear
    Nov 9 at 8:35










  • The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try LocalDate date = rs.getObject("DATE", LocalDate.class) instead? (The java.sql.Date class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
    – Ole V.V.
    Nov 9 at 9:03








  • 1




    Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
    – ReemuS
    Nov 9 at 9:10






  • 1




    Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
    – O. Jones
    Nov 9 at 11:47














  • 3




    Could you add example dates from your DB for both cases, along with the result on java side ?
    – Arnaud
    Nov 9 at 8:35






  • 1




    Just check the timezone for the both DB
    – SaviNuclear
    Nov 9 at 8:35










  • The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try LocalDate date = rs.getObject("DATE", LocalDate.class) instead? (The java.sql.Date class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
    – Ole V.V.
    Nov 9 at 9:03








  • 1




    Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
    – ReemuS
    Nov 9 at 9:10






  • 1




    Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
    – O. Jones
    Nov 9 at 11:47








3




3




Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
Nov 9 at 8:35




Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
Nov 9 at 8:35




1




1




Just check the timezone for the both DB
– SaviNuclear
Nov 9 at 8:35




Just check the timezone for the both DB
– SaviNuclear
Nov 9 at 8:35












The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try LocalDate date = rs.getObject("DATE", LocalDate.class) instead? (The java.sql.Date class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
– Ole V.V.
Nov 9 at 9:03






The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try LocalDate date = rs.getObject("DATE", LocalDate.class) instead? (The java.sql.Date class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
– Ole V.V.
Nov 9 at 9:03






1




1




Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
Nov 9 at 9:10




Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
Nov 9 at 9:10




1




1




Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
Nov 9 at 11:47




Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
Nov 9 at 11:47












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!






share|improve this answer





















  • Very interesting. Thank you.
    – Ole V.V.
    Nov 10 at 19:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222221%2fselect-statement-from-ms-sql-returns-dates-with-loss-of-2-4-days%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!






share|improve this answer





















  • Very interesting. Thank you.
    – Ole V.V.
    Nov 10 at 19:16















up vote
1
down vote













Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!






share|improve this answer





















  • Very interesting. Thank you.
    – Ole V.V.
    Nov 10 at 19:16













up vote
1
down vote










up vote
1
down vote









Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!






share|improve this answer












Solved. I changed DATE fields to DATETIME type in each table. I still have no idea about the "nature" of the problem and why it was so random, but now problem is fixed. Thanks everyone!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 10:39









ReemuS

193




193












  • Very interesting. Thank you.
    – Ole V.V.
    Nov 10 at 19:16


















  • Very interesting. Thank you.
    – Ole V.V.
    Nov 10 at 19:16
















Very interesting. Thank you.
– Ole V.V.
Nov 10 at 19:16




Very interesting. Thank you.
– Ole V.V.
Nov 10 at 19:16


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222221%2fselect-statement-from-ms-sql-returns-dates-with-loss-of-2-4-days%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff