在sql server中格式化日期

时间:2015-10-07 09:22:35

标签: sql-server

这是我的疑问:

SELECT [projectname]
    , CONCAT ([Budget Health]
            , [ProjectBaseline0StartDate
            , [ProjectActualStartDate]
            , [ProjectStartDate]) AS budgethealth
FROM [dbo].[MSP_EpmProject_UserView];

我的输出是:

projectname budgethealth
------------------------
abc         5% varianceApr  7 2015  8:00AMApr  7 2015  8:00AMApr  7 2015  8:00AM

我希望以这种形式获得预算健康信息:

      <5% variance
      Apr/07/2015
      Apr/08/2015
      Apr/08/2015

注意:我希望时间以此格式删除并显示日期。 请帮助。

3 个答案:

答案 0 :(得分:0)

您可以将DateTime格式化为:

  

CONVERT(DATA_TYPE(长度),表达,风格)

例如:

var images = $('.thm-img img'); // get images

// move mouseover onto actual images otherwise you won't know which one is being moused over
images.on('mouseover', function() {
  console.log(images.index($(this)) + 1);  // get index of current image and add 1 (index is 0 based)
});

完整查询:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thumb-wrp">
   <div class="thm-img"><img src="img/img/1.jpg"></div>
   <div class="thm-img"><img src="img/img/2.jpg"></div>
   <div class="thm-img"><img src="img/img/3.jpg"></div>
   <div class="thm-img"><img src="img/img/4.jpg"></div>
   <div class="thm-img"><img src="img/img/1.jpg"></div>
   <div class="thm-img"><img src="img/img/2.jpg"></div>
   <div class="thm-img"><img src="img/img/3.jpg"></div>
   <div class="thm-img"><img src="img/img/4.jpg"></div>
 </div>

请查看详细信息和样式选项: http://www.w3schools.com/sql/func_convert.asp

更新

这是带有convert和default datetime的查询的屏幕截图。 enter image description here

更新2:

如果您想分割线条,可以使用

CONVERT(varchar(11),[ProjectBaseline0StartDate])

您无法在MSSQL查询编辑器和恕我直言中看到查询结果,您不需要它。但是如果您在应用程序中使用此结果,您将获得新行中的值。 例如:

DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
select projectname,CONCAT([Budget Health],@NewLineChar,Convert(varchar(11),[ProjectBaseline0StartDate]),@NewLineChar,CONVERT(varchar(11),[ProjectActualStartDate]),@NewLineChar,CONVERT(varchar(11),[ProjectStartDate])) as budgethealth from [dbo].[MSP_EpmProject_UserView]

有关详细信息:New line in Sql Query

答案 1 :(得分:0)

因为您的代码和输出日期格式不正确。所以我建议您使用逗号分隔CONCAT

所以数据会变成这样的

        DECLARE @Table1 TABLE 
        (projectname varchar(5) , budgethealth varchar(200) )
    ;

    INSERT INTO @Table1
        (projectname, budgethealth)
    VALUES ('abc','5% variance,Apr  7 2015  8:00AM,Apr  7 2015  8:00AM,Apr  7 2015  8:00AM')
;WITH CTE AS (
select   projectname,
         Split.a.value('.', 'VARCHAR(100)') AS SubColour  
     FROM  (SELECT [projectname],
             CAST ('<M>' + REPLACE(budgethealth, ',', '</M><M>') + '</M>' AS XML) AS String  
         FROM  @Table1) AS A CROSS APPLY String.nodes ('/M') AS Split(a) )


         Select projectname,
         CASE WHEN CHARINDEX(':',SubColour) > 0 THEN  
         REPLACE(REPLACE(RTRIM(LTRIM(SUBSTRING(SubColour,-1,CHARINDEX(':',SubColour )))),' ','/'),'//','/') 
         ELSE SubColour
             END from CTE 

答案 2 :(得分:0)

第三个CONVERT参数107为您提供格式&#34; 2015年4月7日&#34;: SELECT CONVERT(varchar(50),[date_column],107)

然后,你可以插入&#34; /&#34;&使用REPLACE删除逗号,一次删除所有字符串。

最终查询:

SELECT [projectname],
  [Budget Health] + CHAR(13) + CHAR(10) + 
  REPLACE(REPLACE(
    CONVERT(char(12), [ProjectBaseline0StartDate], 107) + CHAR(13) + CHAR(10) + 
    CONVERT(char(12), [ProjectActualStartDate], 107) + CHAR(13) + CHAR(10) + 
    CONVERT(char(12), [ProjectStartDate], 107),
  ' ', '/'), ',', '') AS budgethealth
FROM [dbo].[MSP_EpmProject_UserView];