简体   繁体   English

SQL - 插入到 - 转换数据类型

[英]SQL - Insert Into - Convert Datatype

my table consists of the following fields:我的表包含以下字段:

IDTemp int NO NULL
Nr_ nvarchar(50) NULL
Description nvarchar(50) NULL
[Description 2] nvarchar(50) NULL
Price money NULL

Now I want to insert some values into that table from another database, unfortunately the price values from the other database are stored as a nvarchar and not a money value.现在我想从另一个数据库中将一些值插入到该表中,不幸的是,另一个数据库中的价格值存储为nvarchar而不是货币值。 It looks a bit like this: 49.0000000000它看起来有点像这样:49.0000000000

If I insert this value into my money field, it stays empty.如果我将此值插入我的货币字段,它会保持为空。 My question is, how can I use INSERT INTO and convert my price value so that it goes into my money field and doesn't have 10 zeroes?我的问题是,我如何使用 INSERT INTO 并转换我的价格值,以便它进入我的货币字段并且没有 10 个零?

INSERT INTO TempSaveArticle (Nr_, Description, [Description 2], Price)
    VALUES (123456789, Yarn, blue, '49.0000000000')

Thanks for your help in advance提前感谢您的帮助

Use CONVERT() .使用CONVERT()

INSERT INTO TempSaveArticle (Nr_, Description, [Description 2], Price) 
VALUES (123456789, Yarn, blue, CONVERT(MONEY, 49.0000000000))

Ideally you'd want to do this more dynamically.理想情况下,您希望更动态地执行此操作。

INSERT INTO TempSaveArticle (Nr_, Description, [Description 2], Price) 
SELECT myTable.*
FROM myTable
WHERE myTable.ID = 123 OR <<some other conditions>>

MONEY is effectively a DECIMAL . MONEY实际上是一个DECIMAL

You can CAST() or CONVERT() it to and from Decimal <--> Money.您可以将它 CAST() 或 CONVERT() 转换为 Decimal <--> Money。 The only difference I know, is that MONEY is displayed by default using your computers' Language settings.我知道的唯一区别是默认情况下使用计算机的语言设置显示 MONEY。 But in my opinion, don't use MONEY, stick with DECIMALS and display it how you want using FORMAT().但在我看来,不要使用 MONEY,坚持使用 DECIMALS 并使用 FORMAT() 以您想要的方式显示它。 More on this argument here .更多关于这个论点的信息

We have to use cast or convert, this works directly for datatype CHAR, VARCHAR, NCHAR and NVARCHAR.我们必须使用 cast 或 convert,这直接适用于数据类型 CHAR、VARCHAR、NCHAR 和 NVARCHAR。
SQL server will not convert directly from TEXT to money. SQL 服务器不会直接将 TEXT 转换为金钱。 If the datatype is TEXT we have to first cast to varchar(for example) and then cast again to money.如果数据类型是 TEXT,我们必须首先转换为 varchar(例如),然后再次转换为 money。
NB: In some countries, for example France where I leave, it is standard practise to use a comma instead of a point.注意:在某些国家/地区,例如我离开的法国,使用逗号代替点是标准做法。 If there is a possibility of this we would need to use REPLACE first otherwise we will get an error.如果有这种可能性,我们需要先使用 REPLACE 否则我们会得到一个错误。

 create table mon ( m.net text, monev nvarchar(20), m.netm money, monevm money); GO
  
 insert into mon (m.net, monev) values('49.0000000000','49.0000000000'); GO
 

1 rows affected 1 行受影响

select * from mon; GO
 m.net |网| monev |莫涅夫 | m.netm | m.netm | monevm:------------ |:------------ |货币管理:------------ |:------------ | -----: | -----: | -----: 49.0000000000 | -----: 49.0000000000 | 49.0000000000 | 49.0000000000 | null | null | null null
 update mon set monevm = cast(monev as decimal(20,10))from mon; GO
 

1 rows affected 1 行受影响

update mon set m.netm = cast(m.net as decimal(20,10))from mon; GO
 Msg 529 Level 16 State 2 Line 1 Msg 529 Level 16 State 2 Line 1

Explicit conversion from data type text to decimal is not allowed.不允许从数据类型文本到十进制的显式转换。

 update mon set m.netm = cast( cast(m.net as varchar) as decimal(20,10))from mon; GO
 

1 rows affected 1 行受影响

select * from mon; GO
 m.net |网| monev |莫涅夫 | m.netm | m.netm | monevm:------------ |:------------ |货币管理:------------ |:------------ | ------: | ------: | ------: 49.0000000000 | ------: 49.0000000000 | 49.0000000000 | 49.0000000000 | 49.0000 | 49.0000 | 49.0000 49.0000

db<>fiddle here db<> 在这里摆弄

use CAST to convert this properly使用 CAST 正确转换

SELECT CAST ('49.0000000000' as numeric(10,2));

output: output:

49.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM