Convert and Cast for Date and Money format.

SELECT REPLACE(REPLACE(@str, CHAR(13), ''), CHAR(10), '')


The below script removes the TAB(Horozontal Tab), Line feed(New line), Carriage Return Characters in a variable @String

复制代码
SET NOCOUNT ON 
DECLARE @String VARCHAR(100) 
DECLARE @CorrectedString VARCHAR(100) 
SELECT @String = 'AB    C D' 
PRINT @String 
SELECT @CorrectedString = REPLACE(@String, CHAR(9),'') 
PRINT @CorrectedString 
SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(10),'') 
PRINT @CorrectedString 
SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(13),'') 
PRINT @CorrectedString
复制代码

 

Extended script which also provides length of the string:

复制代码
SET NOCOUNT ON 
DECLARE @String VARCHAR(100) 
DECLARE @CorrectedString VARCHAR(100) 
SELECT @String = 'AB    C D' 
PRINT @String 
PRINT 'LENGTH='+CAST(LEN(@String) AS VARCHAR(5)) 
SELECT @CorrectedString = REPLACE(@String, CHAR(9),'') 
PRINT @CorrectedString PRINT 'LENGTH='+CAST(LEN(@CorrectedString) AS VARCHAR(5)) 
SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(10),'') 
PRINT @CorrectedString PRINT 'LENGTH='+CAST(LEN(@CorrectedString) AS VARCHAR(5)) 
SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(13),'') 
PRINT @CorrectedString PRINT 'LENGTH='+CAST(LEN(@CorrectedString) AS VARCHAR(5))
复制代码

I work with a lot of databases (specifically, T-SQL databases) which collect form submissions in which there is a pretty large comment field.
Being that this is a textarea, often people make liberal use of line breaks. Unfortunately, this wreaks havoc when you try to either copy the
results from the query into Excel, or export to a CSV and then import to Excel. Even when you force double quotes around each column,
Excel still happily creates a new row whenever it sees a line break.The solution I found was to modify the SELECT query to
remove the two character entities representing line breaks and new lines in T-SQL,
which are CHAR(13) and CHAR(10). This should cover your bases for the new line characters CR, LF, and CR+LF.

The specific SQL looks like this:

 

SELECT REPLACE(REPLACE(@str, CHAR(13), ' '), CHAR(10), ' ')


select CONVERT(VARCHAR(10) ,getdate(),103) result
union
select CONVERT(VARCHAR(20) ,cast(555666.1258 as money),1);

result
18/10/2014
555,666.13
select isnull(null,'')
select replace(cast(666555.234666 as money),'.',',') --666555,23
select replace(cast(666555.235666 as money),'.',',') --666555,24


http://msdn.microsoft.com/en-us/library/ms187928(v=sql.105).aspx


http://msdn.microsoft.com/en-us/library/hh213505.aspx

posted @   princessd8251  阅读(284)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示