SQL - 插入 - 转换数据类型
我的表包含以下字段:
IDTemp int NO NULL
Nr_ nvarchar(50) NULL
Description nvarchar(50) NULL
[Description 2] nvarchar(50) NULL
Price money NULL
现在我想从另一个数据库将一些值插入到该表中,不幸的是,另一个数据库中的价格值存储为 nvarchar
而不是货币值。 它看起来有点像这样: 49.0000000000
如果我将此值插入到我的钱字段中,它会保持为空。 我的问题是,如何使用 INSERT INTO 并转换我的价格值,以便它进入我的货币字段并且没有 10 个零?
INSERT INTO TempSaveArticle (Nr_, Description, [Description 2], Price)
VALUES (123456789, Yarn, blue, '49.0000000000')
感谢您提前的帮助
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.
It looks a bit like this: 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 TempSaveArticle (Nr_, Description, [Description 2], Price)
VALUES (123456789, Yarn, blue, '49.0000000000')
Thanks for your help in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用CONVERT()。
理想情况下,您希望更动态地执行此操作。
MONEY 实际上是一个十进制。
您可以将其 CAST() 或 CONVERT() 与十进制 <-->钱。我知道的唯一区别是,默认情况下使用计算机的语言设置显示金钱。但在我看来,不要使用金钱,坚持使用小数并使用 FORMAT() 按您想要的方式显示它。有关此论点的更多信息此处。
Use CONVERT().
Ideally you'd want to do this more dynamically.
MONEY is effectively a DECIMAL.
You can CAST() or CONVERT() it to and from Decimal <--> Money. The only difference I know, is that MONEY is displayed by default using your computers' Language settings. But in my opinion, don't use MONEY, stick with DECIMALS and display it how you want using FORMAT(). More on this argument here.
使用 CAST 正确转换
SELECT CAST ('49.0000000000' as numeric(10,2));
输出:
use CAST to convert this properly
SELECT CAST ('49.0000000000' as numeric(10,2));
output :
我们必须使用强制转换或转换,这直接适用于数据类型 CHAR、VARCHAR、NCHAR 和 NVARCHAR。
SQL Server 不会直接从 TEXT 转换为货币。如果数据类型是TEXT,我们必须首先转换为varchar(例如),然后再次转换为money。
注意:在某些国家/地区,例如我离开的法国,使用逗号而不是点是标准做法。如果有可能出现这种情况,我们需要首先使用 REPLACE,否则我们会收到错误。
1 行受影响
1 行受影响
不允许从数据类型文本显式转换为十进制。
1 行受影响
db<>fiddle 此处
We have to use cast or convert, this works directly for datatype CHAR, VARCHAR, NCHAR and NVARCHAR.
SQL server will not convert directly from TEXT to money. If the datatype is TEXT we have to first cast to varchar(for example) and then cast again to 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.
1 rows affected
1 rows affected
Explicit conversion from data type text to decimal is not allowed.
1 rows affected
db<>fiddle here