Postgresql Compare varchar and time type











up vote
0
down vote

favorite












In PostgreSQL, I need to make a LEFT JOIN between two columns that are different in data type. One of them is varchar and the other is time



LEFT JOIN my_table ON r.varchar_time = my_table.time


How can I compare them, by using to_char?










share|improve this question






















  • You really ought to provide some sample data with this question, or we have to guess what it looks like.
    – Used_By_Already
    Nov 19 at 23:54















up vote
0
down vote

favorite












In PostgreSQL, I need to make a LEFT JOIN between two columns that are different in data type. One of them is varchar and the other is time



LEFT JOIN my_table ON r.varchar_time = my_table.time


How can I compare them, by using to_char?










share|improve this question






















  • You really ought to provide some sample data with this question, or we have to guess what it looks like.
    – Used_By_Already
    Nov 19 at 23:54













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In PostgreSQL, I need to make a LEFT JOIN between two columns that are different in data type. One of them is varchar and the other is time



LEFT JOIN my_table ON r.varchar_time = my_table.time


How can I compare them, by using to_char?










share|improve this question













In PostgreSQL, I need to make a LEFT JOIN between two columns that are different in data type. One of them is varchar and the other is time



LEFT JOIN my_table ON r.varchar_time = my_table.time


How can I compare them, by using to_char?







sql postgresql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 22:40









pmirnd

68521129




68521129












  • You really ought to provide some sample data with this question, or we have to guess what it looks like.
    – Used_By_Already
    Nov 19 at 23:54


















  • You really ought to provide some sample data with this question, or we have to guess what it looks like.
    – Used_By_Already
    Nov 19 at 23:54
















You really ought to provide some sample data with this question, or we have to guess what it looks like.
– Used_By_Already
Nov 19 at 23:54




You really ought to provide some sample data with this question, or we have to guess what it looks like.
– Used_By_Already
Nov 19 at 23:54












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Type conversions may use an abbreviated syntax such as ::time so, you may be able to use:



LEFT JOIN my_table ON r.varchar_time::time = my_table.time


However if there are any values that don't convert to time that may error. You might go in the opposite direction, but now you must ensure that the format produced by the varchar conversion matches the other column's format.



LEFT JOIN my_table ON r.varchar_time = my_table.time::varchar


Either way you have potential problems. Ideally that varchar column would be time as well.



The other possible issue I see with this is that time precision might be a problem as well, but this depends on how you populate the columns.



test:



select
(current_timestamp::time)::varchar "varchar"
, current_timestamp::time "time"

+-----------------+------------------+
| varchar | time |
+-----------------+------------------+
| 00:50:20.703713 | 00:50:20.7037130 |
+-----------------+------------------+


As you can see from this test the "format" of your varchar data will matter






share|improve this answer























  • This one works, I have no error but the result of what I'm selecting is empty, but that has to be a problem with my joins. In the end, I had to use this: LEFT JOIN my_table ON extract(hour from r.varchar_time::time) = extract(hour from my_table.time)
    – pmirnd
    Nov 20 at 18:28










  • This was the answer that helped me most. Thanks.
    – pmirnd
    Nov 20 at 19:11


















up vote
1
down vote













Postgres has functions to convert from one datatype to another. From the docs:



to_timestamp(text, text) , timestamp with time zone, converts string to time stamp



Example: to_timestamp('05 Dec 2000', 'DD Mon YYYY')



Your code might look like this, using whichever format you need to match the other column.



LEFT JOIN my_table ON to_timestamp(r.varchar_time, 'HH:mm:ss') = my_table.time






share|improve this answer





















  • I got this: [Err] ERROR: operator doesn't exists: timestamp with time zone = time without time zone
    – pmirnd
    Nov 19 at 22:50











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%2f53383686%2fpostgresql-compare-varchar-and-time-type%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Type conversions may use an abbreviated syntax such as ::time so, you may be able to use:



LEFT JOIN my_table ON r.varchar_time::time = my_table.time


However if there are any values that don't convert to time that may error. You might go in the opposite direction, but now you must ensure that the format produced by the varchar conversion matches the other column's format.



LEFT JOIN my_table ON r.varchar_time = my_table.time::varchar


Either way you have potential problems. Ideally that varchar column would be time as well.



The other possible issue I see with this is that time precision might be a problem as well, but this depends on how you populate the columns.



test:



select
(current_timestamp::time)::varchar "varchar"
, current_timestamp::time "time"

+-----------------+------------------+
| varchar | time |
+-----------------+------------------+
| 00:50:20.703713 | 00:50:20.7037130 |
+-----------------+------------------+


As you can see from this test the "format" of your varchar data will matter






share|improve this answer























  • This one works, I have no error but the result of what I'm selecting is empty, but that has to be a problem with my joins. In the end, I had to use this: LEFT JOIN my_table ON extract(hour from r.varchar_time::time) = extract(hour from my_table.time)
    – pmirnd
    Nov 20 at 18:28










  • This was the answer that helped me most. Thanks.
    – pmirnd
    Nov 20 at 19:11















up vote
1
down vote



accepted










Type conversions may use an abbreviated syntax such as ::time so, you may be able to use:



LEFT JOIN my_table ON r.varchar_time::time = my_table.time


However if there are any values that don't convert to time that may error. You might go in the opposite direction, but now you must ensure that the format produced by the varchar conversion matches the other column's format.



LEFT JOIN my_table ON r.varchar_time = my_table.time::varchar


Either way you have potential problems. Ideally that varchar column would be time as well.



The other possible issue I see with this is that time precision might be a problem as well, but this depends on how you populate the columns.



test:



select
(current_timestamp::time)::varchar "varchar"
, current_timestamp::time "time"

+-----------------+------------------+
| varchar | time |
+-----------------+------------------+
| 00:50:20.703713 | 00:50:20.7037130 |
+-----------------+------------------+


As you can see from this test the "format" of your varchar data will matter






share|improve this answer























  • This one works, I have no error but the result of what I'm selecting is empty, but that has to be a problem with my joins. In the end, I had to use this: LEFT JOIN my_table ON extract(hour from r.varchar_time::time) = extract(hour from my_table.time)
    – pmirnd
    Nov 20 at 18:28










  • This was the answer that helped me most. Thanks.
    – pmirnd
    Nov 20 at 19:11













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Type conversions may use an abbreviated syntax such as ::time so, you may be able to use:



LEFT JOIN my_table ON r.varchar_time::time = my_table.time


However if there are any values that don't convert to time that may error. You might go in the opposite direction, but now you must ensure that the format produced by the varchar conversion matches the other column's format.



LEFT JOIN my_table ON r.varchar_time = my_table.time::varchar


Either way you have potential problems. Ideally that varchar column would be time as well.



The other possible issue I see with this is that time precision might be a problem as well, but this depends on how you populate the columns.



test:



select
(current_timestamp::time)::varchar "varchar"
, current_timestamp::time "time"

+-----------------+------------------+
| varchar | time |
+-----------------+------------------+
| 00:50:20.703713 | 00:50:20.7037130 |
+-----------------+------------------+


As you can see from this test the "format" of your varchar data will matter






share|improve this answer














Type conversions may use an abbreviated syntax such as ::time so, you may be able to use:



LEFT JOIN my_table ON r.varchar_time::time = my_table.time


However if there are any values that don't convert to time that may error. You might go in the opposite direction, but now you must ensure that the format produced by the varchar conversion matches the other column's format.



LEFT JOIN my_table ON r.varchar_time = my_table.time::varchar


Either way you have potential problems. Ideally that varchar column would be time as well.



The other possible issue I see with this is that time precision might be a problem as well, but this depends on how you populate the columns.



test:



select
(current_timestamp::time)::varchar "varchar"
, current_timestamp::time "time"

+-----------------+------------------+
| varchar | time |
+-----------------+------------------+
| 00:50:20.703713 | 00:50:20.7037130 |
+-----------------+------------------+


As you can see from this test the "format" of your varchar data will matter







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 23:53

























answered Nov 19 at 23:46









Used_By_Already

22.2k21838




22.2k21838












  • This one works, I have no error but the result of what I'm selecting is empty, but that has to be a problem with my joins. In the end, I had to use this: LEFT JOIN my_table ON extract(hour from r.varchar_time::time) = extract(hour from my_table.time)
    – pmirnd
    Nov 20 at 18:28










  • This was the answer that helped me most. Thanks.
    – pmirnd
    Nov 20 at 19:11


















  • This one works, I have no error but the result of what I'm selecting is empty, but that has to be a problem with my joins. In the end, I had to use this: LEFT JOIN my_table ON extract(hour from r.varchar_time::time) = extract(hour from my_table.time)
    – pmirnd
    Nov 20 at 18:28










  • This was the answer that helped me most. Thanks.
    – pmirnd
    Nov 20 at 19:11
















This one works, I have no error but the result of what I'm selecting is empty, but that has to be a problem with my joins. In the end, I had to use this: LEFT JOIN my_table ON extract(hour from r.varchar_time::time) = extract(hour from my_table.time)
– pmirnd
Nov 20 at 18:28




This one works, I have no error but the result of what I'm selecting is empty, but that has to be a problem with my joins. In the end, I had to use this: LEFT JOIN my_table ON extract(hour from r.varchar_time::time) = extract(hour from my_table.time)
– pmirnd
Nov 20 at 18:28












This was the answer that helped me most. Thanks.
– pmirnd
Nov 20 at 19:11




This was the answer that helped me most. Thanks.
– pmirnd
Nov 20 at 19:11












up vote
1
down vote













Postgres has functions to convert from one datatype to another. From the docs:



to_timestamp(text, text) , timestamp with time zone, converts string to time stamp



Example: to_timestamp('05 Dec 2000', 'DD Mon YYYY')



Your code might look like this, using whichever format you need to match the other column.



LEFT JOIN my_table ON to_timestamp(r.varchar_time, 'HH:mm:ss') = my_table.time






share|improve this answer





















  • I got this: [Err] ERROR: operator doesn't exists: timestamp with time zone = time without time zone
    – pmirnd
    Nov 19 at 22:50















up vote
1
down vote













Postgres has functions to convert from one datatype to another. From the docs:



to_timestamp(text, text) , timestamp with time zone, converts string to time stamp



Example: to_timestamp('05 Dec 2000', 'DD Mon YYYY')



Your code might look like this, using whichever format you need to match the other column.



LEFT JOIN my_table ON to_timestamp(r.varchar_time, 'HH:mm:ss') = my_table.time






share|improve this answer





















  • I got this: [Err] ERROR: operator doesn't exists: timestamp with time zone = time without time zone
    – pmirnd
    Nov 19 at 22:50













up vote
1
down vote










up vote
1
down vote









Postgres has functions to convert from one datatype to another. From the docs:



to_timestamp(text, text) , timestamp with time zone, converts string to time stamp



Example: to_timestamp('05 Dec 2000', 'DD Mon YYYY')



Your code might look like this, using whichever format you need to match the other column.



LEFT JOIN my_table ON to_timestamp(r.varchar_time, 'HH:mm:ss') = my_table.time






share|improve this answer












Postgres has functions to convert from one datatype to another. From the docs:



to_timestamp(text, text) , timestamp with time zone, converts string to time stamp



Example: to_timestamp('05 Dec 2000', 'DD Mon YYYY')



Your code might look like this, using whichever format you need to match the other column.



LEFT JOIN my_table ON to_timestamp(r.varchar_time, 'HH:mm:ss') = my_table.time







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 22:46









Adrian J. Moreno

10k12634




10k12634












  • I got this: [Err] ERROR: operator doesn't exists: timestamp with time zone = time without time zone
    – pmirnd
    Nov 19 at 22:50


















  • I got this: [Err] ERROR: operator doesn't exists: timestamp with time zone = time without time zone
    – pmirnd
    Nov 19 at 22:50
















I got this: [Err] ERROR: operator doesn't exists: timestamp with time zone = time without time zone
– pmirnd
Nov 19 at 22:50




I got this: [Err] ERROR: operator doesn't exists: timestamp with time zone = time without time zone
– pmirnd
Nov 19 at 22:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383686%2fpostgresql-compare-varchar-and-time-type%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

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga