Why the value is constant?
I would like to make a procedure which updates birth date, basing on personal identity number and everything works well apart from one thing. PID is taken only once and consequently the result is the same for all 200 records. How should I change this?
For example: PID of the first value is 30052766293, so birth_date = 1930-05-27,but for left PIDs result (date) is the same
CREATE PROCEDURE proc()
BEGIN
DECLARE s INT DEFAULT 0;
abc:LOOP
UPDATE people
SET birth_date = (SELECT str_to_date(CONCAT('19',substring(PID,1,2),
substring(PID,3,2),
substring(PID,5,2)
)
, '%Y %m %d') FROM workers LIMIT 1);
SET s = s+1;
IF s>=200
THEN LEAVE abc;
END IF ;
END LOOP;
END $$
EDIT: people and workers are separate tables and both have the same column PID
mysql
|
show 1 more comment
I would like to make a procedure which updates birth date, basing on personal identity number and everything works well apart from one thing. PID is taken only once and consequently the result is the same for all 200 records. How should I change this?
For example: PID of the first value is 30052766293, so birth_date = 1930-05-27,but for left PIDs result (date) is the same
CREATE PROCEDURE proc()
BEGIN
DECLARE s INT DEFAULT 0;
abc:LOOP
UPDATE people
SET birth_date = (SELECT str_to_date(CONCAT('19',substring(PID,1,2),
substring(PID,3,2),
substring(PID,5,2)
)
, '%Y %m %d') FROM workers LIMIT 1);
SET s = s+1;
IF s>=200
THEN LEAVE abc;
END IF ;
END LOOP;
END $$
EDIT: people and workers are separate tables and both have the same column PID
mysql
So if you havePID
inpeople
already, why do you need to get it fromworkers
?
– Nick
Nov 18 '18 at 7:08
Do you only want to change 200 records or all of them?
– Nick
Nov 18 '18 at 7:14
I benefit from this convenience because when I try to 'SELECT FROM' the same table I get error like 'Can't specify target...'
– Mark
Nov 18 '18 at 7:16
There are exactly 200 records
– Mark
Nov 18 '18 at 7:17
If there were more or less than 200, would you want to change all of them or just 200?
– Nick
Nov 18 '18 at 7:18
|
show 1 more comment
I would like to make a procedure which updates birth date, basing on personal identity number and everything works well apart from one thing. PID is taken only once and consequently the result is the same for all 200 records. How should I change this?
For example: PID of the first value is 30052766293, so birth_date = 1930-05-27,but for left PIDs result (date) is the same
CREATE PROCEDURE proc()
BEGIN
DECLARE s INT DEFAULT 0;
abc:LOOP
UPDATE people
SET birth_date = (SELECT str_to_date(CONCAT('19',substring(PID,1,2),
substring(PID,3,2),
substring(PID,5,2)
)
, '%Y %m %d') FROM workers LIMIT 1);
SET s = s+1;
IF s>=200
THEN LEAVE abc;
END IF ;
END LOOP;
END $$
EDIT: people and workers are separate tables and both have the same column PID
mysql
I would like to make a procedure which updates birth date, basing on personal identity number and everything works well apart from one thing. PID is taken only once and consequently the result is the same for all 200 records. How should I change this?
For example: PID of the first value is 30052766293, so birth_date = 1930-05-27,but for left PIDs result (date) is the same
CREATE PROCEDURE proc()
BEGIN
DECLARE s INT DEFAULT 0;
abc:LOOP
UPDATE people
SET birth_date = (SELECT str_to_date(CONCAT('19',substring(PID,1,2),
substring(PID,3,2),
substring(PID,5,2)
)
, '%Y %m %d') FROM workers LIMIT 1);
SET s = s+1;
IF s>=200
THEN LEAVE abc;
END IF ;
END LOOP;
END $$
EDIT: people and workers are separate tables and both have the same column PID
mysql
mysql
edited Nov 18 '18 at 7:10
Mark
asked Nov 18 '18 at 6:43
MarkMark
83
83
So if you havePID
inpeople
already, why do you need to get it fromworkers
?
– Nick
Nov 18 '18 at 7:08
Do you only want to change 200 records or all of them?
– Nick
Nov 18 '18 at 7:14
I benefit from this convenience because when I try to 'SELECT FROM' the same table I get error like 'Can't specify target...'
– Mark
Nov 18 '18 at 7:16
There are exactly 200 records
– Mark
Nov 18 '18 at 7:17
If there were more or less than 200, would you want to change all of them or just 200?
– Nick
Nov 18 '18 at 7:18
|
show 1 more comment
So if you havePID
inpeople
already, why do you need to get it fromworkers
?
– Nick
Nov 18 '18 at 7:08
Do you only want to change 200 records or all of them?
– Nick
Nov 18 '18 at 7:14
I benefit from this convenience because when I try to 'SELECT FROM' the same table I get error like 'Can't specify target...'
– Mark
Nov 18 '18 at 7:16
There are exactly 200 records
– Mark
Nov 18 '18 at 7:17
If there were more or less than 200, would you want to change all of them or just 200?
– Nick
Nov 18 '18 at 7:18
So if you have
PID
in people
already, why do you need to get it from workers
?– Nick
Nov 18 '18 at 7:08
So if you have
PID
in people
already, why do you need to get it from workers
?– Nick
Nov 18 '18 at 7:08
Do you only want to change 200 records or all of them?
– Nick
Nov 18 '18 at 7:14
Do you only want to change 200 records or all of them?
– Nick
Nov 18 '18 at 7:14
I benefit from this convenience because when I try to 'SELECT FROM' the same table I get error like 'Can't specify target...'
– Mark
Nov 18 '18 at 7:16
I benefit from this convenience because when I try to 'SELECT FROM' the same table I get error like 'Can't specify target...'
– Mark
Nov 18 '18 at 7:16
There are exactly 200 records
– Mark
Nov 18 '18 at 7:17
There are exactly 200 records
– Mark
Nov 18 '18 at 7:17
If there were more or less than 200, would you want to change all of them or just 200?
– Nick
Nov 18 '18 at 7:18
If there were more or less than 200, would you want to change all of them or just 200?
– Nick
Nov 18 '18 at 7:18
|
show 1 more comment
1 Answer
1
active
oldest
votes
As you already have the PID
value in your people
table, you can simply change your UPDATE
to use that value. Note that since you want to change all the values in the table, you don't even need a stored procedure, you can just run this query directly:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,2),
SUBSTRING(PID,3,2),
SUBSTRING(PID,5,2)
)
, '%Y %m %d');
Note that it looks like you could further simplify this to:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,6))
, '%Y%m%d');
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%2f53358530%2fwhy-the-value-is-constant%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
As you already have the PID
value in your people
table, you can simply change your UPDATE
to use that value. Note that since you want to change all the values in the table, you don't even need a stored procedure, you can just run this query directly:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,2),
SUBSTRING(PID,3,2),
SUBSTRING(PID,5,2)
)
, '%Y %m %d');
Note that it looks like you could further simplify this to:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,6))
, '%Y%m%d');
add a comment |
As you already have the PID
value in your people
table, you can simply change your UPDATE
to use that value. Note that since you want to change all the values in the table, you don't even need a stored procedure, you can just run this query directly:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,2),
SUBSTRING(PID,3,2),
SUBSTRING(PID,5,2)
)
, '%Y %m %d');
Note that it looks like you could further simplify this to:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,6))
, '%Y%m%d');
add a comment |
As you already have the PID
value in your people
table, you can simply change your UPDATE
to use that value. Note that since you want to change all the values in the table, you don't even need a stored procedure, you can just run this query directly:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,2),
SUBSTRING(PID,3,2),
SUBSTRING(PID,5,2)
)
, '%Y %m %d');
Note that it looks like you could further simplify this to:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,6))
, '%Y%m%d');
As you already have the PID
value in your people
table, you can simply change your UPDATE
to use that value. Note that since you want to change all the values in the table, you don't even need a stored procedure, you can just run this query directly:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,2),
SUBSTRING(PID,3,2),
SUBSTRING(PID,5,2)
)
, '%Y %m %d');
Note that it looks like you could further simplify this to:
UPDATE people
SET birth_date = STR_TO_DATE(CONCAT('19',SUBSTRING(PID,1,6))
, '%Y%m%d');
answered Nov 18 '18 at 7:20
NickNick
26.6k111839
26.6k111839
add a comment |
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%2f53358530%2fwhy-the-value-is-constant%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
So if you have
PID
inpeople
already, why do you need to get it fromworkers
?– Nick
Nov 18 '18 at 7:08
Do you only want to change 200 records or all of them?
– Nick
Nov 18 '18 at 7:14
I benefit from this convenience because when I try to 'SELECT FROM' the same table I get error like 'Can't specify target...'
– Mark
Nov 18 '18 at 7:16
There are exactly 200 records
– Mark
Nov 18 '18 at 7:17
If there were more or less than 200, would you want to change all of them or just 200?
– Nick
Nov 18 '18 at 7:18