• 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

  • 相关阅读:
    用Service充当Domain Object
    Scrum方法回顾
    为什么使用User Story Map
    前端状态管理之状态机
    项目进度管理注意事项
    单元测试遇到的最难的问题
    JS AMD模块的循环依赖
    jupyter notebook常用快捷键
    Jupyter-NoteBook-你应该知道的N个小技巧
    Python之配置日志的几种方式(logging模块)
  • 原文地址:https://www.cnblogs.com/princessd8251/p/4031341.html
Copyright © 2020-2023  润新知