SQL Server日期转换问题

时间:2016-08-22 21:20:32

标签: sql-server

我有一张表格,上面有这种格式的日期。 YYYYDDD

我需要将它们转换为YYYY-MM格式

例如,我有一个日期2016003,因此应该成为2016-01

同样,2016055的日期应为2016-02

有没有办法在不创建用户功能的情况下显示这些内容?

谢谢

1 个答案:

答案 0 :(得分:3)

我认为最简单的方法是将日期添加到年初。所以,要获得日期:

<div id="Container">
    <div class="boxTop"></div>
    <div class="box"><table width="300px">
<tbody>
<tr>
<td>
<table cellspacing="0" cellpadding="6">
<tbody>
<tr>
<td valign="top"><a href="http://ejgh.org/your-health/healthy-lifestyles/become-a-member-sp-1672965733"><img style="border-width: 0px;" src="http://ejgh.org/images/stories/template/healthylifestyles/apple.jpg" alt="Image of Apple and Weights for homepage" width="86" height="86" /></a></td>
<td valign="top">
<h3><a href="/your-health/healthy-lifestyles/become-a-member-sp-1672965733">Become a Member</a></h3>
<p>Join Healthy Lifestyles and enjoy the benefits of membership.</p>
</td>
</tr>
<tr>
<td valign="top"><a href="/component/wrapper/?Itemid=203"><img style="border-width: 0px;" src="http://ejgh.org/images/stories/template/healthylifestyles/elderlycouple.jpg" alt="Image of elderly couple at hospital in New Orleans Louisiana" width="86" height="83" /></a></td>
<td valign="top">
<h3><a href="/your-health/healthy-lifestyles/classes-support">Classes &amp; Support</a></h3>
<p>Learn more about the classes, support groups, and health screenings we offer.</p>
</td>
</tr>
<tr>
<td valign="top"><a href="/component/hwdvideoshare/?task=viewcategory&amp;Itemid=166&amp;cat_id=5"><img style="border-width: 0px;" src="http://ejgh.org/images/stories/template/healthylifestyles/tvspot.jpg" alt="Image of Liz Delsa Healthy Lifestyles Host" width="86" height="70" /></a></td>
<td valign="top">
<h3><a href="/your-health/healthy-lifestyles/healthy-lifestyles-tv">Watch the TV Segment</a></h3>
<p>Watch Healthy Lifestyles TV segments as seen on WWL-TV.</p>
</td>
</tr>
<tr>
<td valign="top"><a href="/your-health/healthy-lifestyles/healthy-lifestyles-magazine"><img src="http://ejgh.org/images/stories/yourhealthimages/healthylifestyles/MagazineCovers/summer2016.jpg" alt="Summer 2016 Healthy Lifestyles Cover" width="86" height="100" /></a></td>
<td valign="top">
<h3><a href="/your-health/healthy-lifestyles/healthy-lifestyles-magazine">Read the Magazine</a></h3>
<p>Read the latest Healthy Lifestyles Magazine as included in the Times-Picayune newspaper.</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
          </table></div>
    <div class="boxBtm"></div>
 </div>

然后您可以根据需要对其进行格式化。例如,在SQL Server 2012+中,您可以使用select dateadd(day, cast(right(col, 3) as int) - 1, cast(left(col, 4) + '01-01' as date) ) as dte

format()