[英]Changing column from nvarchar to int and converting data
I have a Users table that is used to hold data for a user's gender in nvarchar(6) format. 我有一个Users表,用于以nvarchar(6)格式保存用户性别的数据。 I changed the software to use enums to indicate gender and thus wish to save data as an int.
我将软件更改为使用枚举来表示性别,因此希望将数据另存为int。
0 = Male, 1 = Female. 0 =男性,1 =女性。
I put each username and old nvarchar gender in a separate table and have converted the gender column in the original Users table to an int column. 我将每个用户名和旧的nvarchar性别分别放在单独的表中,并将原始Users表中的性别列转换为int列。 How can I set my query up to take all the users in my temp table and match them by user name to users in the Users table and then set the gender = 0 or 1 depending on if the old value is "Female" or "Male"?
如何设置查询以获取临时表中的所有用户,并通过用户名将其与Users表中的用户进行匹配,然后将性别设置为0或1,具体取决于旧值是“女性”还是“男性” “?
Users 用户数
id int PK id int PK
Username nvarchar (50) 用户名nvarchar (50)
Gender int 性别诠释
tempTable 临时表
id int PK (Clusted Index w/no relation to the User's PK) id int PK(聚类索引,与用户的PK不相关)
username nvarchar (50) 用户名nvarchar (50)
gender nvarchar (6) 性别nvarchar (6)
I want to match users in tempTable
by username
to users in Users
table by Username
and update the Users.Gender
value to either 0 or 1 based on tempTable.gender
being either male or female. 我想匹配的用户
tempTable
通过username
到用户Users
通过表中Username
和更新Users.Gender
值基于0或1 tempTable.gender
是男性或女性。
Something like this should work: 这样的事情应该起作用:
update a set gender = case b.Gender
when 'Male' then 0
when 'Female' then 1
end
from users a inner join
tempTable b on b.Username = a.Username
This assumes every 'old' value is either Male or Female, and doesn't account for bad/missing data. 假设每个“旧”值都是“男性”或“女性”,并且不考虑错误/丢失的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.