0

## 將 varchar 值 轉換成資料類型 int 時，轉換失敗。

COND01為主KEY

``````
INSERT INTO MDCOND (COND01,COND02,COND03,COND04,COND05) VALUES ( (convert(int,'COND04'+'COND05')) ,'123','456','78','12');

``````

### 4 個回答

0

iT邦大師 1 級 ‧ 2018-07-07 16:20:40

script解決方式可以參考D大的。

``````CREATE TABLE MDCOND
([COND01]  AS Convert(int,COND04) + Convert(int,COND05)  , [COND02] varchar(5), [COND03] varchar(5), [COND04] varchar(5), [COND05] varchar(5))
;

insert into MDCOND (COND02,COND03,COND04,COND05) values ('123','456','78','12');
select * from MDCOND;
``````

insert資料時候添加COND02,COND03,COND04,COND05就好

z22344566 iT邦新手 4 級 ‧ 2018-07-07 16:43:17 檢舉

0

iT邦研究生 2 級 ‧ 2018-07-07 16:22:07

`'COND04'+'COND05'` 這段字串含有非整數字串 無法轉型成 `int`整數

``````CREATE TABLE MDCOND(
COND01  INT,
COND02  VARCHAR(10),
COND03  VARCHAR(10),
COND04  VARCHAR(10),
COND05  VARCHAR(10)
);

DECLARE @COND04 VARCHAR(10) = '78'
DECLARE @COND05 VARCHAR(10) = '12'

INSERT INTO MDCOND (COND01,COND02,COND03,COND04,COND05)
VALUES ( (convert(int,@COND04+@COND05)) ,'123','456',@COND04,@COND05);
``````

dbfiddle

z22344566 iT邦新手 4 級 ‧ 2018-07-07 16:40:39 檢舉

``````    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

SqlDataSource1.InsertCommand = "INSERT INTO MDCOND (COND01,COND02,COND03,COND04,COND05) " & _
"VALUES((convert(INT,@COND01)),@COND02,@COND03,@COND04,'1') "

Try

If SqlDataSource1.Insert() = 1 Then

CON_STATUS.Text = "成功!"
ClearTextBox(Page)

Else
CON_STATUS.Text = "新增失敗"

End If
Catch
CON_STATUS.Text = "<font style='color: #FF3300'>" + "新增失敗  " + "</font>"

End Try

End Sub
``````

TXT_CONA02.Text + TXT_CONA04.Text

z22344566 iT邦新手 4 級 ‧ 2018-07-08 18:05:07 檢舉

TEXT剛讀出來式字串，要轉型態INT

0
sonia
iT邦新手 3 級 ‧ 2018-07-09 09:47:52

INSERT INTO MDCOND (COND01,COND02,COND03,COND04,COND05) VALUES ( (select COND04 +COND05 from xxxx where ...) ,'123','456','78','12');

0
grande
iT邦新手 5 級 ‧ 2018-07-09 12:52:06

INSERT INTO MDCOND (COND01,COND02,COND03,COND04,COND05) VALUES(0 ,'123','456','78','12');

UPDATE MDCOND SET COND01=(convert(int,COND04+COND05));