使用 SQL 服务器将字符插入 INT 字段
Inserting characters into an INT field with SQL Server
我在 SQL fiddle 中创建了一个 fiddle 并尝试显示数字 1-99 的列表。如果满足某些条件,我还希望将数字替换为文本。我已经实现了条件但无法输入文本,因为该列是一个 INT 字段。我曾尝试在出现这些情况之前使用 CAST,但这只会导致 SQL fiddle 崩溃。我也尝试将整个列更改为 VARCHAR,但我遇到了相同的结果。
感谢任何帮助! link 到 fiddle 如下。
http://sqlfiddle.com/#!18/691f2/1
编辑工作代码
CREATE TABLE numbers(
numbers int
);
DECLARE @start as int
SET @start = 1
DECLARE @plus as int
SET @plus = 1
DECLARE @end as int
SET @end = 99
BEGIN TRANSACTION
WHILE (@start <= @end)
BEGIN INSERT INTO numbers
values (@start) SET @start += @plus
END COMMIT TRANSACTION;
UPDATE numbers
SET numbers = '3737'
WHERE numbers%3 = 0 AND numbers%7 = 0;
UPDATE numbers
SET numbers = '3333'
WHERE numbers%3 = 0;
UPDATE numbers
SET numbers = '7777'
WHERE numbers%7 = 0;
SELECT (CASE numbers
WHEN 3333 THEN 'Open'
WHEN 7777 THEN 'Source'
WHEN 3737 THEN 'OpenSource'
ELSE CAST(numbers as VARCHAR)
END) AS Numbers
FROM numbers;
What if I made two columns, how would I go about merging them?
如果要将 int
列和 varchar
列的值合并为一列,则必须将 int
转换为 varchar
,因为字符串值不能转换为 int
.
我在 SQL fiddle 中创建了一个 fiddle 并尝试显示数字 1-99 的列表。如果满足某些条件,我还希望将数字替换为文本。我已经实现了条件但无法输入文本,因为该列是一个 INT 字段。我曾尝试在出现这些情况之前使用 CAST,但这只会导致 SQL fiddle 崩溃。我也尝试将整个列更改为 VARCHAR,但我遇到了相同的结果。
感谢任何帮助! link 到 fiddle 如下。
http://sqlfiddle.com/#!18/691f2/1
编辑工作代码
CREATE TABLE numbers(
numbers int
);
DECLARE @start as int
SET @start = 1
DECLARE @plus as int
SET @plus = 1
DECLARE @end as int
SET @end = 99
BEGIN TRANSACTION
WHILE (@start <= @end)
BEGIN INSERT INTO numbers
values (@start) SET @start += @plus
END COMMIT TRANSACTION;
UPDATE numbers
SET numbers = '3737'
WHERE numbers%3 = 0 AND numbers%7 = 0;
UPDATE numbers
SET numbers = '3333'
WHERE numbers%3 = 0;
UPDATE numbers
SET numbers = '7777'
WHERE numbers%7 = 0;
SELECT (CASE numbers
WHEN 3333 THEN 'Open'
WHEN 7777 THEN 'Source'
WHEN 3737 THEN 'OpenSource'
ELSE CAST(numbers as VARCHAR)
END) AS Numbers
FROM numbers;
What if I made two columns, how would I go about merging them?
如果要将 int
列和 varchar
列的值合并为一列,则必须将 int
转换为 varchar
,因为字符串值不能转换为 int
.