MySQL Select Query - Get only first 10 characters of a value
Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl
.
Columns - id
, subject
, value
.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
mysql sql select
add a comment |
Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl
.
Columns - id
, subject
, value
.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
mysql sql select
add a comment |
Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl
.
Columns - id
, subject
, value
.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
mysql sql select
Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl
.
Columns - id
, subject
, value
.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
mysql sql select
mysql sql select
edited Sep 13 '17 at 8:06
span
3,66933982
3,66933982
asked Feb 19 '13 at 13:51
getvivekvgetvivekv
8882818
8882818
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
add a comment |
SELECT SUBSTRING(subject, 1, 10) FROM tbl
I feel like this is the more complete answer, sinceLEFT
may not address specifics (yes, like those raised by the OP) dealing with extractions that need to start mid-string.
– d8aninja
Oct 17 '18 at 20:28
add a comment |
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
3
A sample would have been a bit more helpful.
– Rocco The Taco
Mar 11 '14 at 14:46
6
@RoccoTheTaco I totally disagree - also your down vote is very harsh. readhttp://stackoverflow.com/questions/how-to-ask
the very first point isHave you thoroughly searched for an answer before asking your question?
. This question is so easily answered by a simple and quick Google search. I didn't just want to give the OP the answer I wanted to show them HOW to find the answer - much more useful in my opinion.
– Steve
Mar 11 '14 at 16:42
10
I understand what @Steve is saying (makes sense) but I came here from Google. Yes, the original question was lazy but the fact that I don't have to go somewhere else helped me. It's also nice that SO is building up a library of answers to simple questions like these. Also equals more advertising $ for SO.
– sterfry68
Dec 31 '14 at 19:49
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%2f14959166%2fmysql-select-query-get-only-first-10-characters-of-a-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
add a comment |
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
add a comment |
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
answered Feb 19 '13 at 13:54
MuhammadHaniMuhammadHani
6,61642344
6,61642344
add a comment |
add a comment |
SELECT SUBSTRING(subject, 1, 10) FROM tbl
I feel like this is the more complete answer, sinceLEFT
may not address specifics (yes, like those raised by the OP) dealing with extractions that need to start mid-string.
– d8aninja
Oct 17 '18 at 20:28
add a comment |
SELECT SUBSTRING(subject, 1, 10) FROM tbl
I feel like this is the more complete answer, sinceLEFT
may not address specifics (yes, like those raised by the OP) dealing with extractions that need to start mid-string.
– d8aninja
Oct 17 '18 at 20:28
add a comment |
SELECT SUBSTRING(subject, 1, 10) FROM tbl
SELECT SUBSTRING(subject, 1, 10) FROM tbl
answered Oct 24 '13 at 7:24
Rajesh PaulRajesh Paul
3,80062642
3,80062642
I feel like this is the more complete answer, sinceLEFT
may not address specifics (yes, like those raised by the OP) dealing with extractions that need to start mid-string.
– d8aninja
Oct 17 '18 at 20:28
add a comment |
I feel like this is the more complete answer, sinceLEFT
may not address specifics (yes, like those raised by the OP) dealing with extractions that need to start mid-string.
– d8aninja
Oct 17 '18 at 20:28
I feel like this is the more complete answer, since
LEFT
may not address specifics (yes, like those raised by the OP) dealing with extractions that need to start mid-string.– d8aninja
Oct 17 '18 at 20:28
I feel like this is the more complete answer, since
LEFT
may not address specifics (yes, like those raised by the OP) dealing with extractions that need to start mid-string.– d8aninja
Oct 17 '18 at 20:28
add a comment |
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
3
A sample would have been a bit more helpful.
– Rocco The Taco
Mar 11 '14 at 14:46
6
@RoccoTheTaco I totally disagree - also your down vote is very harsh. readhttp://stackoverflow.com/questions/how-to-ask
the very first point isHave you thoroughly searched for an answer before asking your question?
. This question is so easily answered by a simple and quick Google search. I didn't just want to give the OP the answer I wanted to show them HOW to find the answer - much more useful in my opinion.
– Steve
Mar 11 '14 at 16:42
10
I understand what @Steve is saying (makes sense) but I came here from Google. Yes, the original question was lazy but the fact that I don't have to go somewhere else helped me. It's also nice that SO is building up a library of answers to simple questions like these. Also equals more advertising $ for SO.
– sterfry68
Dec 31 '14 at 19:49
add a comment |
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
3
A sample would have been a bit more helpful.
– Rocco The Taco
Mar 11 '14 at 14:46
6
@RoccoTheTaco I totally disagree - also your down vote is very harsh. readhttp://stackoverflow.com/questions/how-to-ask
the very first point isHave you thoroughly searched for an answer before asking your question?
. This question is so easily answered by a simple and quick Google search. I didn't just want to give the OP the answer I wanted to show them HOW to find the answer - much more useful in my opinion.
– Steve
Mar 11 '14 at 16:42
10
I understand what @Steve is saying (makes sense) but I came here from Google. Yes, the original question was lazy but the fact that I don't have to go somewhere else helped me. It's also nice that SO is building up a library of answers to simple questions like these. Also equals more advertising $ for SO.
– sterfry68
Dec 31 '14 at 19:49
add a comment |
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
answered Feb 19 '13 at 13:55
SteveSteve
3,01211221
3,01211221
3
A sample would have been a bit more helpful.
– Rocco The Taco
Mar 11 '14 at 14:46
6
@RoccoTheTaco I totally disagree - also your down vote is very harsh. readhttp://stackoverflow.com/questions/how-to-ask
the very first point isHave you thoroughly searched for an answer before asking your question?
. This question is so easily answered by a simple and quick Google search. I didn't just want to give the OP the answer I wanted to show them HOW to find the answer - much more useful in my opinion.
– Steve
Mar 11 '14 at 16:42
10
I understand what @Steve is saying (makes sense) but I came here from Google. Yes, the original question was lazy but the fact that I don't have to go somewhere else helped me. It's also nice that SO is building up a library of answers to simple questions like these. Also equals more advertising $ for SO.
– sterfry68
Dec 31 '14 at 19:49
add a comment |
3
A sample would have been a bit more helpful.
– Rocco The Taco
Mar 11 '14 at 14:46
6
@RoccoTheTaco I totally disagree - also your down vote is very harsh. readhttp://stackoverflow.com/questions/how-to-ask
the very first point isHave you thoroughly searched for an answer before asking your question?
. This question is so easily answered by a simple and quick Google search. I didn't just want to give the OP the answer I wanted to show them HOW to find the answer - much more useful in my opinion.
– Steve
Mar 11 '14 at 16:42
10
I understand what @Steve is saying (makes sense) but I came here from Google. Yes, the original question was lazy but the fact that I don't have to go somewhere else helped me. It's also nice that SO is building up a library of answers to simple questions like these. Also equals more advertising $ for SO.
– sterfry68
Dec 31 '14 at 19:49
3
3
A sample would have been a bit more helpful.
– Rocco The Taco
Mar 11 '14 at 14:46
A sample would have been a bit more helpful.
– Rocco The Taco
Mar 11 '14 at 14:46
6
6
@RoccoTheTaco I totally disagree - also your down vote is very harsh. read
http://stackoverflow.com/questions/how-to-ask
the very first point is Have you thoroughly searched for an answer before asking your question?
. This question is so easily answered by a simple and quick Google search. I didn't just want to give the OP the answer I wanted to show them HOW to find the answer - much more useful in my opinion.– Steve
Mar 11 '14 at 16:42
@RoccoTheTaco I totally disagree - also your down vote is very harsh. read
http://stackoverflow.com/questions/how-to-ask
the very first point is Have you thoroughly searched for an answer before asking your question?
. This question is so easily answered by a simple and quick Google search. I didn't just want to give the OP the answer I wanted to show them HOW to find the answer - much more useful in my opinion.– Steve
Mar 11 '14 at 16:42
10
10
I understand what @Steve is saying (makes sense) but I came here from Google. Yes, the original question was lazy but the fact that I don't have to go somewhere else helped me. It's also nice that SO is building up a library of answers to simple questions like these. Also equals more advertising $ for SO.
– sterfry68
Dec 31 '14 at 19:49
I understand what @Steve is saying (makes sense) but I came here from Google. Yes, the original question was lazy but the fact that I don't have to go somewhere else helped me. It's also nice that SO is building up a library of answers to simple questions like these. Also equals more advertising $ for SO.
– sterfry68
Dec 31 '14 at 19:49
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%2f14959166%2fmysql-select-query-get-only-first-10-characters-of-a-value%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