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
Convert and Cast for Date and Money format.的更多相关文章
- SQL Server 2008 Datetime Cast 成 Date 类型可以使用索引(转载)
很久没写blog,不是懒,实在是最近我这的访问速度不好,用firefox经常上传不了图片 ....... 今天无意发现了SQL Server 2008 Datetime Cast 成 Date 类型可 ...
- mysql类型转换函数convert与cast的用法
原文地址:https://blog.csdn.net/kouwoo/article/details/45535733 简单介绍下mysql数据库中用于类型转换的二个函数,convert与cast函数, ...
- Convert a byte[] array to readable string format. This makes the "hex" readable!
/* * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol ...
- date命令转换日期命令提示date: illegal time format
问题:运行date命令抛错 date -j -f "%a %b %d %T %Z %Y" "Sat Sep 29 11:33:00 CST 2018" &quo ...
- sql 中convert和cast区别
SQL中的cast和convert的用法和区别 更多 来源:SQL学习浏览量: 学习标签: cast convert sql 本文导读:SQL中的cast 和convert都是用来将一种数据类型的表达 ...
- mysql类型转换函数convert与cast的用法,及SQL server的区别
首先,convert函数 字符集转换 : CONVERT(xxx USING gb2312) 类型转换和SQL Server一样,不过类型参数上有不同: CAST(xxx AS 类型) ...
- 使用date类和format类对系统当前时间进行格式化显示
一:Date------------String 代码1:(代码二对显示出来的时间格式进行优化) package DateDemo; import java.text.SimpleDateFormat ...
- date命令的FORMAT中输入空格的几种方法
1.date +%Y-%m-%d\ (一个空格)%H:%M:%S 此命令中用了转义字符 \ ,将空格转义出来 2.date +%Y-%m-%d' '%H:%M:%S 此命令中的单引号内可以是一个或多 ...
- "Date has wrong format. Use one of these formats instead: %, Y, -, %, m, -, %, d." DateField使用input_formats参数
错误写法 : publish_date = serializers.DateField(format="%Y-%m-%d", input_formats="%Y-%m-% ...
随机推荐
- (转)Array.prototype.slice.call自解
很多框架或者库里面都会有这句的使用,最多的还是通过Array.prototype.slice.call(arguments,0)把arguments这个伪数组转换为真正的数组.但为什么可以这么做,却一 ...
- CUDA程序设计(二)
算法设计:直方图统计 直方图频数统计,也可以看成一个字典Hash计数.用处不是很多,但是涉及CUDA核心操作:全局内存.共享内存.原子函数. 1.1 基本串行算法 这只是一个C语言练习题. #def ...
- Android数据存储的三种方式:SharePreferences , file , SQLite
(1)SharePreferences: 存入: SharedPreferences setter = this.getSharedPreferences("spfile", 0) ...
- Leetcode Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Codeforce - Rock-Paper-Scissors
Rock-Paper-Scissors is a two-player game, where each player chooses one of Rock, Paper, or Scissors. ...
- 关于GC垃圾回收的原理
.NET Framework 并不需要担心垃圾回收.但我们还是需要了解它的原理.才能让我们写出更高效的应用程序. .Net Framework 有一个GC(垃圾回收器),它会自动的帮我们把不需要的数据 ...
- Android在智能终端领域的关键技术专题讲座(成都站)
Android系统引领了终端智能化的浪潮,在民用.公 共及工业等诸多领域得到了广泛的应用,涉及手持终端.电视.汽车导航.工业控制等,在云计算.设备智能化等方面表现卓越.Android也凭借着自身的优 ...
- .net后台获取HTML中select元素选中的值
前台: <select id="Province" name="Province" class="select"></se ...
- Git初使用
今天开始初次使用Git,Git作为一个使用广泛的分布式版本控制系统,我们有必要熟悉掌握. 这次主要是实现将本地上的“Hello World”的完整的项目文件提交到github上新建的代码库,主要过程如 ...
- java: cairo-misc.c:380: _cairo_operator_bounded_by_source: Assertion `NOT_REACHED' failed.
出错原因 该问题会在Centos6.6及更高版本出现.也会在其他版本中出现. 解决方案 禁用carioGraphics > Add -Dorg.eclipse.swt.internal.gtk. ...