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

时间: 2024-10-29 19:09:57

Convert and Cast for Date and Money format.的相关文章

SQL Server 2008 Datetime Cast 成 Date 类型可以使用索引(转载)

很久没写blog,不是懒,实在是最近我这的访问速度不好,用firefox经常上传不了图片 ....... 今天无意发现了SQL Server 2008 Datetime Cast 成 Date 类型可以使用索引,分享一下: 测试环境: USE TEMPDBGO CREATE TABLE TB(ID INT IDENTITY(1,1) PRIMARY KEY,NAME VARCHAR(200),OPTIME DATETIME DEFAULT GETDATE())GODECLARE @I INT =

date命令转换日期命令提示date: illegal time format

问题:运行date命令抛错 date -j -f "%a %b %d %T %Z %Y" "Sat Sep 29 11:33:00 CST 2018" "+%s" 报错: Failed conversion of ``Sat Sep 29 11:33:00 CST 2018'' using format ``%a %b %d %T %Z %Y'' date: illegal time format usage: date [-jnRu] [-d

sql 中convert和cast区别

SQL中的cast和convert的用法和区别 更多 2014/1/13 来源:SQL学习浏览量:14125 学习标签: cast convert sql 本文导读:SQL中的cast 和convert都是用来将一种数据类型的表达式转换为另一种数据类型的表达式.CAST 和 CONVERT 提供相似的功能,只是语法不同.在时间转化中一般用到convert,因为它比cast多加了一个style,可以转化成不同时间的格式. 一.语法: 1.使用 CAST CAST ( expression AS d

mysql类型转换函数convert与cast的用法,及SQL server的区别

首先,convert函数 字符集转换 :   CONVERT(xxx  USING   gb2312) 类型转换和SQL Server一样,不过类型参数上有不同: CAST(xxx  AS   类型), CONVERT(xxx,类型),类型必须用下列的类型: 可用的类型      二进制,同带binary前缀的效果 : BINARY      字符型,可带参数 : CHAR()       日期 : DATE       时间: TIME       日期时间型 : DATETIME      

使用date类和format类对系统当前时间进行格式化显示

一:Date------------String 代码1:(代码二对显示出来的时间格式进行优化) package DateDemo; import java.text.SimpleDateFormat; import java.util.Date; // 需求:将现在系统的时间打印出来 // 需要的类:Date 类:生成当前系统时间 // SimpleDateFormat 类:对生成的系统时间进行格式化 // 构造方法摘要 SimpleDateFormat() public class Date

DATE_FORMAT(date, format)

DATE_FORMAT(date, format) 用于格式化日期,即根据 format 指定的格式显示 date 值,format 格式 mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y'); +------------------------------------------------+ | DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y') | +---------------

数据库字符串、日期及数据类型转换(cast /convert)

数据库(字符串函数,日期函数,数据类型转换CAST,convert) use student go create table yuangong ( code int, name varchar(20), sex varchar(20), age int, bumen varchar(20), ) insert into  yuangong  values(1,'周数','男',27,'销售部') insert into  yuangong  values(1,'周的王','男',27,'销售部'

Ext.util.Format.date与Ext.Date.format区别, 转换时间戳

在Extjs中装时间戳使用如下两种都可以: Ext.util.Format.date(time,'U'); Ext.Date.format(time, 'U'); 为了找到它们的区别,查看源代码,以Extjs 4.2 为例 Ext.util.Format.date -> \src\util\Format.js 258 date: function(v, format) { if (!v) { return ""; } if (!Ext.isDate(v)) { v = new D

java 日期操作,Date、Calendar 操作

java开发中避免不了日期相关操作,这里总结了一些常用方法~ 直接上码: package jse; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; /**  * 常用日期操