Formatting true/false in sql
I am passing a dict that I get from mysql directly into json. Unfortunately, the json fails when trying to parse the boolean with the following error:
'Current token (VALUE_NUMBER_INT) not of boolean type
Is the following the preferred way to cast a TINYINT(1)
to a javascript boolean?
if(tm.is_live, 'true', 'false') live_for_preorder,
mysql sql json
|
show 2 more comments
I am passing a dict that I get from mysql directly into json. Unfortunately, the json fails when trying to parse the boolean with the following error:
'Current token (VALUE_NUMBER_INT) not of boolean type
Is the following the preferred way to cast a TINYINT(1)
to a javascript boolean?
if(tm.is_live, 'true', 'false') live_for_preorder,
mysql sql json
What will be the possible values intm.is_live
field ?
– Madhur Bhaiya
Nov 15 '18 at 4:46
This does not look like MySQL error. But it is looking as errror from Elastic Search: github.com/elastic/elasticsearch/issues/10056
– Madhur Bhaiya
Nov 15 '18 at 4:47
How did you get the dict? What MySQL version? What table structure/query?
– danblack
Nov 15 '18 at 4:50
@MadhurBhaiya thetm.is_live
field is a TINYINT(1) and the values will be either0
or1
. I think json (ES) is excepting 'true'/'false'.
– David L
Nov 15 '18 at 4:52
1
@DavidL your existing query should work. You can also convertIf()
to ANSI SQL compliantCASE..WHEN
(if you change DB later, asIF()
is MySQL specific only). In case ofCASE..WHEN
, it would look as follows:CASE tm.is_live WHEN 0 THEN 'false' ELSE 'true' END AS live_for_preorder
– Madhur Bhaiya
Nov 15 '18 at 5:21
|
show 2 more comments
I am passing a dict that I get from mysql directly into json. Unfortunately, the json fails when trying to parse the boolean with the following error:
'Current token (VALUE_NUMBER_INT) not of boolean type
Is the following the preferred way to cast a TINYINT(1)
to a javascript boolean?
if(tm.is_live, 'true', 'false') live_for_preorder,
mysql sql json
I am passing a dict that I get from mysql directly into json. Unfortunately, the json fails when trying to parse the boolean with the following error:
'Current token (VALUE_NUMBER_INT) not of boolean type
Is the following the preferred way to cast a TINYINT(1)
to a javascript boolean?
if(tm.is_live, 'true', 'false') live_for_preorder,
mysql sql json
mysql sql json
edited Nov 15 '18 at 4:53
David L
asked Nov 15 '18 at 4:43
David LDavid L
11617
11617
What will be the possible values intm.is_live
field ?
– Madhur Bhaiya
Nov 15 '18 at 4:46
This does not look like MySQL error. But it is looking as errror from Elastic Search: github.com/elastic/elasticsearch/issues/10056
– Madhur Bhaiya
Nov 15 '18 at 4:47
How did you get the dict? What MySQL version? What table structure/query?
– danblack
Nov 15 '18 at 4:50
@MadhurBhaiya thetm.is_live
field is a TINYINT(1) and the values will be either0
or1
. I think json (ES) is excepting 'true'/'false'.
– David L
Nov 15 '18 at 4:52
1
@DavidL your existing query should work. You can also convertIf()
to ANSI SQL compliantCASE..WHEN
(if you change DB later, asIF()
is MySQL specific only). In case ofCASE..WHEN
, it would look as follows:CASE tm.is_live WHEN 0 THEN 'false' ELSE 'true' END AS live_for_preorder
– Madhur Bhaiya
Nov 15 '18 at 5:21
|
show 2 more comments
What will be the possible values intm.is_live
field ?
– Madhur Bhaiya
Nov 15 '18 at 4:46
This does not look like MySQL error. But it is looking as errror from Elastic Search: github.com/elastic/elasticsearch/issues/10056
– Madhur Bhaiya
Nov 15 '18 at 4:47
How did you get the dict? What MySQL version? What table structure/query?
– danblack
Nov 15 '18 at 4:50
@MadhurBhaiya thetm.is_live
field is a TINYINT(1) and the values will be either0
or1
. I think json (ES) is excepting 'true'/'false'.
– David L
Nov 15 '18 at 4:52
1
@DavidL your existing query should work. You can also convertIf()
to ANSI SQL compliantCASE..WHEN
(if you change DB later, asIF()
is MySQL specific only). In case ofCASE..WHEN
, it would look as follows:CASE tm.is_live WHEN 0 THEN 'false' ELSE 'true' END AS live_for_preorder
– Madhur Bhaiya
Nov 15 '18 at 5:21
What will be the possible values in
tm.is_live
field ?– Madhur Bhaiya
Nov 15 '18 at 4:46
What will be the possible values in
tm.is_live
field ?– Madhur Bhaiya
Nov 15 '18 at 4:46
This does not look like MySQL error. But it is looking as errror from Elastic Search: github.com/elastic/elasticsearch/issues/10056
– Madhur Bhaiya
Nov 15 '18 at 4:47
This does not look like MySQL error. But it is looking as errror from Elastic Search: github.com/elastic/elasticsearch/issues/10056
– Madhur Bhaiya
Nov 15 '18 at 4:47
How did you get the dict? What MySQL version? What table structure/query?
– danblack
Nov 15 '18 at 4:50
How did you get the dict? What MySQL version? What table structure/query?
– danblack
Nov 15 '18 at 4:50
@MadhurBhaiya the
tm.is_live
field is a TINYINT(1) and the values will be either 0
or 1
. I think json (ES) is excepting 'true'/'false'.– David L
Nov 15 '18 at 4:52
@MadhurBhaiya the
tm.is_live
field is a TINYINT(1) and the values will be either 0
or 1
. I think json (ES) is excepting 'true'/'false'.– David L
Nov 15 '18 at 4:52
1
1
@DavidL your existing query should work. You can also convert
If()
to ANSI SQL compliant CASE..WHEN
(if you change DB later, as IF()
is MySQL specific only). In case of CASE..WHEN
, it would look as follows: CASE tm.is_live WHEN 0 THEN 'false' ELSE 'true' END AS live_for_preorder
– Madhur Bhaiya
Nov 15 '18 at 5:21
@DavidL your existing query should work. You can also convert
If()
to ANSI SQL compliant CASE..WHEN
(if you change DB later, as IF()
is MySQL specific only). In case of CASE..WHEN
, it would look as follows: CASE tm.is_live WHEN 0 THEN 'false' ELSE 'true' END AS live_for_preorder
– Madhur Bhaiya
Nov 15 '18 at 5:21
|
show 2 more comments
2 Answers
2
active
oldest
votes
If you want to convert to a MySQL boolean, simply do:
select (tm.is_live <> 0) as live_for_preorder,
If this is accepted by Javascript, then this would be the simplest solution.
add a comment |
I seem to recall MySQL is one of those rare databases that lets you use IF in normal SELECT statements where most dbms would insist on case, IF being reserved for control flow
As such it appears that MySQL is telling you you’re not providing IF with a Boolean when it wants one. Convert your statement to a comparison that returns a Boolean:
if(tm.is_live=1, 'true', 'false') live_for_preorder,
(If you’re using 1 for true and 0 for false)
To make your db skill set more portable it would be good to get into the habit of using CASE rather than IF in select queries:
CASE tm.is_live WHEN 1 THEN 'true' ELSE 'false' END live_for_preorder,
CASE WHEN tm.is_live = 1 THEN 'true' ELSE 'false' END live_for_preorder,
A case can have any number of WHEN, but you can’t mix the forms, it’s either CASE variable WHEN comparevalue THEN outputvalue [when when when else] END
Or it’s CASE WHEN test THEN output [when when when else] END
Sql server will allow you to use iif(condition, result if true, result if false) in a select statement but it is very rarely used (at least from what I have read/seen). docs.microsoft.com/en-us/sql/t-sql/functions/…
– Dwight Reynoldson
Nov 15 '18 at 7:33
1
That's implemented as a function though, right? And you can't use IIF for control flow? All major dbms I know of insist on CASE for assignment and IF for flow, except MySQL allows either for both (dirty!)
– Caius Jard
Nov 15 '18 at 7:38
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312550%2fformatting-true-false-in-sql%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
If you want to convert to a MySQL boolean, simply do:
select (tm.is_live <> 0) as live_for_preorder,
If this is accepted by Javascript, then this would be the simplest solution.
add a comment |
If you want to convert to a MySQL boolean, simply do:
select (tm.is_live <> 0) as live_for_preorder,
If this is accepted by Javascript, then this would be the simplest solution.
add a comment |
If you want to convert to a MySQL boolean, simply do:
select (tm.is_live <> 0) as live_for_preorder,
If this is accepted by Javascript, then this would be the simplest solution.
If you want to convert to a MySQL boolean, simply do:
select (tm.is_live <> 0) as live_for_preorder,
If this is accepted by Javascript, then this would be the simplest solution.
answered Nov 15 '18 at 12:50
Gordon LinoffGordon Linoff
781k35310414
781k35310414
add a comment |
add a comment |
I seem to recall MySQL is one of those rare databases that lets you use IF in normal SELECT statements where most dbms would insist on case, IF being reserved for control flow
As such it appears that MySQL is telling you you’re not providing IF with a Boolean when it wants one. Convert your statement to a comparison that returns a Boolean:
if(tm.is_live=1, 'true', 'false') live_for_preorder,
(If you’re using 1 for true and 0 for false)
To make your db skill set more portable it would be good to get into the habit of using CASE rather than IF in select queries:
CASE tm.is_live WHEN 1 THEN 'true' ELSE 'false' END live_for_preorder,
CASE WHEN tm.is_live = 1 THEN 'true' ELSE 'false' END live_for_preorder,
A case can have any number of WHEN, but you can’t mix the forms, it’s either CASE variable WHEN comparevalue THEN outputvalue [when when when else] END
Or it’s CASE WHEN test THEN output [when when when else] END
Sql server will allow you to use iif(condition, result if true, result if false) in a select statement but it is very rarely used (at least from what I have read/seen). docs.microsoft.com/en-us/sql/t-sql/functions/…
– Dwight Reynoldson
Nov 15 '18 at 7:33
1
That's implemented as a function though, right? And you can't use IIF for control flow? All major dbms I know of insist on CASE for assignment and IF for flow, except MySQL allows either for both (dirty!)
– Caius Jard
Nov 15 '18 at 7:38
add a comment |
I seem to recall MySQL is one of those rare databases that lets you use IF in normal SELECT statements where most dbms would insist on case, IF being reserved for control flow
As such it appears that MySQL is telling you you’re not providing IF with a Boolean when it wants one. Convert your statement to a comparison that returns a Boolean:
if(tm.is_live=1, 'true', 'false') live_for_preorder,
(If you’re using 1 for true and 0 for false)
To make your db skill set more portable it would be good to get into the habit of using CASE rather than IF in select queries:
CASE tm.is_live WHEN 1 THEN 'true' ELSE 'false' END live_for_preorder,
CASE WHEN tm.is_live = 1 THEN 'true' ELSE 'false' END live_for_preorder,
A case can have any number of WHEN, but you can’t mix the forms, it’s either CASE variable WHEN comparevalue THEN outputvalue [when when when else] END
Or it’s CASE WHEN test THEN output [when when when else] END
Sql server will allow you to use iif(condition, result if true, result if false) in a select statement but it is very rarely used (at least from what I have read/seen). docs.microsoft.com/en-us/sql/t-sql/functions/…
– Dwight Reynoldson
Nov 15 '18 at 7:33
1
That's implemented as a function though, right? And you can't use IIF for control flow? All major dbms I know of insist on CASE for assignment and IF for flow, except MySQL allows either for both (dirty!)
– Caius Jard
Nov 15 '18 at 7:38
add a comment |
I seem to recall MySQL is one of those rare databases that lets you use IF in normal SELECT statements where most dbms would insist on case, IF being reserved for control flow
As such it appears that MySQL is telling you you’re not providing IF with a Boolean when it wants one. Convert your statement to a comparison that returns a Boolean:
if(tm.is_live=1, 'true', 'false') live_for_preorder,
(If you’re using 1 for true and 0 for false)
To make your db skill set more portable it would be good to get into the habit of using CASE rather than IF in select queries:
CASE tm.is_live WHEN 1 THEN 'true' ELSE 'false' END live_for_preorder,
CASE WHEN tm.is_live = 1 THEN 'true' ELSE 'false' END live_for_preorder,
A case can have any number of WHEN, but you can’t mix the forms, it’s either CASE variable WHEN comparevalue THEN outputvalue [when when when else] END
Or it’s CASE WHEN test THEN output [when when when else] END
I seem to recall MySQL is one of those rare databases that lets you use IF in normal SELECT statements where most dbms would insist on case, IF being reserved for control flow
As such it appears that MySQL is telling you you’re not providing IF with a Boolean when it wants one. Convert your statement to a comparison that returns a Boolean:
if(tm.is_live=1, 'true', 'false') live_for_preorder,
(If you’re using 1 for true and 0 for false)
To make your db skill set more portable it would be good to get into the habit of using CASE rather than IF in select queries:
CASE tm.is_live WHEN 1 THEN 'true' ELSE 'false' END live_for_preorder,
CASE WHEN tm.is_live = 1 THEN 'true' ELSE 'false' END live_for_preorder,
A case can have any number of WHEN, but you can’t mix the forms, it’s either CASE variable WHEN comparevalue THEN outputvalue [when when when else] END
Or it’s CASE WHEN test THEN output [when when when else] END
edited Nov 15 '18 at 7:35
answered Nov 15 '18 at 7:24
Caius JardCaius Jard
12k21240
12k21240
Sql server will allow you to use iif(condition, result if true, result if false) in a select statement but it is very rarely used (at least from what I have read/seen). docs.microsoft.com/en-us/sql/t-sql/functions/…
– Dwight Reynoldson
Nov 15 '18 at 7:33
1
That's implemented as a function though, right? And you can't use IIF for control flow? All major dbms I know of insist on CASE for assignment and IF for flow, except MySQL allows either for both (dirty!)
– Caius Jard
Nov 15 '18 at 7:38
add a comment |
Sql server will allow you to use iif(condition, result if true, result if false) in a select statement but it is very rarely used (at least from what I have read/seen). docs.microsoft.com/en-us/sql/t-sql/functions/…
– Dwight Reynoldson
Nov 15 '18 at 7:33
1
That's implemented as a function though, right? And you can't use IIF for control flow? All major dbms I know of insist on CASE for assignment and IF for flow, except MySQL allows either for both (dirty!)
– Caius Jard
Nov 15 '18 at 7:38
Sql server will allow you to use iif(condition, result if true, result if false) in a select statement but it is very rarely used (at least from what I have read/seen). docs.microsoft.com/en-us/sql/t-sql/functions/…
– Dwight Reynoldson
Nov 15 '18 at 7:33
Sql server will allow you to use iif(condition, result if true, result if false) in a select statement but it is very rarely used (at least from what I have read/seen). docs.microsoft.com/en-us/sql/t-sql/functions/…
– Dwight Reynoldson
Nov 15 '18 at 7:33
1
1
That's implemented as a function though, right? And you can't use IIF for control flow? All major dbms I know of insist on CASE for assignment and IF for flow, except MySQL allows either for both (dirty!)
– Caius Jard
Nov 15 '18 at 7:38
That's implemented as a function though, right? And you can't use IIF for control flow? All major dbms I know of insist on CASE for assignment and IF for flow, except MySQL allows either for both (dirty!)
– Caius Jard
Nov 15 '18 at 7:38
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312550%2fformatting-true-false-in-sql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What will be the possible values in
tm.is_live
field ?– Madhur Bhaiya
Nov 15 '18 at 4:46
This does not look like MySQL error. But it is looking as errror from Elastic Search: github.com/elastic/elasticsearch/issues/10056
– Madhur Bhaiya
Nov 15 '18 at 4:47
How did you get the dict? What MySQL version? What table structure/query?
– danblack
Nov 15 '18 at 4:50
@MadhurBhaiya the
tm.is_live
field is a TINYINT(1) and the values will be either0
or1
. I think json (ES) is excepting 'true'/'false'.– David L
Nov 15 '18 at 4:52
1
@DavidL your existing query should work. You can also convert
If()
to ANSI SQL compliantCASE..WHEN
(if you change DB later, asIF()
is MySQL specific only). In case ofCASE..WHEN
, it would look as follows:CASE tm.is_live WHEN 0 THEN 'false' ELSE 'true' END AS live_for_preorder
– Madhur Bhaiya
Nov 15 '18 at 5:21