简体   繁体   中英

How can I add GETDATE() command on ntext field?

I would like to add dates on ntext field.

UPDATE Customer SET Notes='Account has been updated'+ CAST GETDATE()

This is what I get when I run the query.

"Operand type clash: datetime is incompatible with ntext"

Can anyone help me with this?

您需要将日期强制转换varchar,以便可以将其与您拥有的text / varchar串联。

'Account has been updated'+ CAST(GETDATE() AS varchar(20))

You need to cast the result of GETDATE() to a string to get this to work. The simplest option is to do something like this:

UPDATE Customer SET Notes='Account has been updated'+ CAST(GETDATE() AS NVARCHAR(20))

However if you want control over how the date/time is formatted then use CONVERT instead:

UPDATE Customer SET Notes='Account has been updated'+ CONVERT(NVARCHAR(30), GETDATE(), 126)

The 3rd parameter determines the format to use and the full list of available options for this can be seen on this MSDN page

I would use CONVERT instead of CAST in this case to control the output format.

SELECT 'Account has been updated '+ CONVERT(varchar(20),GETDATE(),104)

This eg give me dd.mm.yyyy.

See full list here: https://msdn.microsoft.com/de-de/library/ms187928%28v=sql.120%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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