April 20, 2017 at 1:06 pm
Hi
Any help on how to update gender = M or F in a table that has gender values as null
need a random insert in to table.
April 20, 2017 at 1:24 pm
db8 - Thursday, April 20, 2017 1:06 PMHiAny help on how to update gender = M or F in a table that has gender values as null
need a random insert in to table.
How would you define if it should be M or F?
April 20, 2017 at 1:29 pm
I believe that this will meet your requirements
WITH T(N) AS (SELECT N FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS X(N))
, NUMS(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS N FROM T T1,T T2,T T3,T T4)
UPDATE src
SET Gender = CASE WHEN ABS(CHECKSUM(NEWID())) %2 = 0 THEN 'F' ELSE 'M' END
FROM NUMS AS NM
INNER JOIN <TableName> AS src ON NM.N = src.id
WHERE src.Gender IS NULL;
April 20, 2017 at 1:30 pm
General advice - don't. There's no way from the name you're going to get the gender right. If that's a customers table, then doing so is sooner or later going to cause problems, sending a letter with the wrong title, for example (and titles are a whole nother pile of potential problems)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 20, 2017 at 1:55 pm
I agree with GilaMonster. Don't update user data without the help of a data steward.
However, if you're simply trying to mock up some test data, this is a simple approach I've used. Use a function against data in the table to generate virtually random results.
CREATE TABLE Star (FName VARCHAR(10) NOT NULL, Gender CHAR(1) NULL)
INSERT INTO Star (FName) VALUES ('Seamus'), ('Cassie'), ('Maria'), ('Jerrod'), ('Xander'), ('Edmond')
UPDATE Star
SET Gender =
CASE ASCII(s.FName)%2
WHEN 0 THEN 'M'
ELSE 'F'
END
FROM
Star s
SELECT Fname, Gender FROM Star
The data set in the results illustrates GilaMonster's point. Some of the data looks right, but any unnoticed inconsistencies will cause headaches later.
April 20, 2017 at 2:11 pm
Kaye Cahs - Thursday, April 20, 2017 1:55 PMHowever, if you're simply trying to mock up some test data, this is a simple approach I've used. Use a function against data in the table to generate virtually random results.
CREATE TABLE Star (FName VARCHAR(10) NOT NULL, Gender CHAR(1) NULL)INSERT INTO Star (FName) VALUES ('Seamus'), ('Cassie'), ('Maria'), ('Jerrod'), ('Xander'), ('Edmond')
UPDATE Star
SET Gender =
CASE ASCII(s.FName)%2
WHEN 0 THEN 'M'
ELSE 'F'
END
FROM
Star s
Or
UPDATE Star
SET Gender =
CASE WHEN RAND(CHECKSUM(NEWID())) > 0.5 THEN 'M' ELSE 'F' END
Completely random assignment. Fine for test data.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 20, 2017 at 5:30 pm
@GilaMonster
Hi sir, i agree with you , but i am using this data only for test purposes.
April 21, 2017 at 1:04 am
Personally, I believe each of those images should be redacted. The data looks to be real enough that it could cause problems.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 21, 2017 at 1:15 am
SQLRNNR - Friday, April 21, 2017 1:04 AMPersonally, I believe each of those images should be redacted. The data looks to be real enough that it could cause problems.
It looks real but that's certainly not a UK phone number and I'm pretty sure Connecticut isn't on this side of the pond.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
April 21, 2017 at 1:41 am
Neil Burton - Friday, April 21, 2017 1:15 AMSQLRNNR - Friday, April 21, 2017 1:04 AMPersonally, I believe each of those images should be redacted. The data looks to be real enough that it could cause problems.It looks real but that's certainly not a UK phone number and I'm pretty sure Connecticut isn't on this side of the pond.
True the country, city and state seem to be randomized. The combination of the phone number with DOB was too eerily close to info I was able to google.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply