insert control characters in nvarchar or varchar from SQL script?

落爺英雄遲暮 提交于 2019-12-10 19:56:57

问题


Given SQL Server 2012, how can I insert control characters (the ones coded under ASCII 32, like TAB, CR, LF) in a nvarchar or varchar column from a SQL script?


回答1:


Unless I miss something in the question you can do this using TSQL CHAR() function:

INSERT INTO MyTable(ColName) VALUES(CHAR(13) + CHAR(10))

Will insert CR/LF. Same for other codes.

Edit there is TSQL NCHAR() as well for Unicode characters.




回答2:


Please note that the function may vary depending on the type of your column, using the wrong function can result in wrong encoding.

nchar/nvarchar http://technet.microsoft.com/en-us/library/ms186939.aspx

char/varchar http://technet.microsoft.com/en-us/library/ms176089.aspx



来源:https://stackoverflow.com/questions/20748811/insert-control-characters-in-nvarchar-or-varchar-from-sql-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!