October 17, 2011 at 12:12 pm
I have a table enployee and a column gender.
The possible values in the table are 'm' or 'f'
In this puzzle i need to swap m with f and f with m in just one sql statement.
Any ideas?
October 17, 2011 at 12:17 pm
ekant_alone (10/17/2011)
I have a table enployee and a column gender.The possible values in the table are 'm' or 'f'
In this puzzle i need to swap m with f and f with m in just one sql statement.
Any ideas?
UPDATE employee
SET gender = new.gender
FROM employee e
INNER JOIN (SELECT EmpId, CASE WHEN gender = 'f' THEN 'm' WHEN gender = 'm' THEN 'f' END) new
ON e.Empid = new.empId
untested
Thanks,
Jared
Jared
CE - Microsoft
October 17, 2011 at 12:18 pm
Sounds like a home work question, so this is a very very basic SQL update with case statement. You should be able to start there.
October 17, 2011 at 12:24 pm
ekant_alone (10/17/2011)
I have a table enployee and a column gender.The possible values in the table are 'm' or 'f'
In this puzzle i need to swap m with f and f with m in just one sql statement.
Any ideas?
CREATE table enployee (gender CHAR(1), oldgender CHAR(1))
INSERT INTO enployee (gender, oldgender) SELECT 'F', 'F'
INSERT INTO enployee (gender, oldgender) SELECT 'F', 'F'
INSERT INTO enployee (gender, oldgender) SELECT 'M', 'M'
INSERT INTO enployee (gender, oldgender) SELECT 'F', 'F'
INSERT INTO enployee (gender, oldgender) SELECT 'M', 'M'
INSERT INTO enployee (gender, oldgender) SELECT 'M', 'M'
SELECT * FROM enployee
UPDATE enployee SET gender = CASE WHEN gender = 'F' THEN 'M' WHEN gender = 'M' THEN 'F' END
SELECT * FROM enployee
You won't learn without putting in some effort.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
October 17, 2011 at 12:28 pm
ChrisM@home (10/17/2011)
ekant_alone (10/17/2011)
I have a table enployee and a column gender.The possible values in the table are 'm' or 'f'
In this puzzle i need to swap m with f and f with m in just one sql statement.
Any ideas?
CREATE table enployee (gender CHAR(1), oldgender CHAR(1))
INSERT INTO enployee (gender, oldgender) SELECT 'F', 'F'
INSERT INTO enployee (gender, oldgender) SELECT 'F', 'F'
INSERT INTO enployee (gender, oldgender) SELECT 'M', 'M'
INSERT INTO enployee (gender, oldgender) SELECT 'F', 'F'
INSERT INTO enployee (gender, oldgender) SELECT 'M', 'M'
INSERT INTO enployee (gender, oldgender) SELECT 'M', 'M'
SELECT * FROM enployee
UPDATE enployee SET gender = CASE WHEN gender = 'F' THEN 'M' WHEN gender = 'M' THEN 'F' END
SELECT * FROM enployee
You won't learn without putting in some effort.
Yeah... not sure why I did it the way I did. Ugh, Mondays... 😉
Jared
Jared
CE - Microsoft
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply