如何更改数据类型

时间:2017-01-04 06:49:16

标签: sql sql-server sql-server-2008

我的记录如下

entrydate
20161231
20161230
20161229
20161228

我需要如下记录

entrydate
2016-12-31 00:00:00.000
2016-12-30 00:00:00.000
2016-12-29 00:00:00.000
2016-12-28 00:00:00.000

6 个答案:

答案 0 :(得分:1)

使用可以使用CONVERT(),如:

select CONVERT(datetime,entrydate,108)

答案 1 :(得分:1)

你需要使用如下的强制转换功能

.navbar-toggle {
	background: #000;
}

如果您使用的是Sql Server 2012版本,请使用以下查询

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<style>
.navbar-toggle {
	background: #000;
    float:left;
}
#social-icons {
	position: absolute;
	top: 15px;
	right: 15px;
}
</style>
<div class="col-sm-8">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse"> <span class="sr-only">Toggle  navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
  </div>
  <!-- navbar-header -->
  
  <nav class="collapse navbar-collapse navigation" id="navbar-collapse">
    <ul class="nav navbar-nav menu">
      <li><a href="/">Home</a></li>
      <li><a href="blog.html">Features</a></li>
      <li><a href="resources.html">Lifestyle</a></li>
      <li><a href="contact.html">Travel</a></li>
      <li><a href="contact.html">Music</a></li>
      <li><a href="contact.html">About</a></li>
      <li><a href="contact.html">Contact Me</a></li>
    </ul>
  </nav>
</div>
<!-- navbar-collapse -->

<div class="col-sm-4 navigation" id="social-icons">
  <ul class="menu">
    <a href=""><i class="fa fa-facebook"></i></a> <a href=""><i class="fa fa-twitter"></i></a> <a href=""><i class="fa fa-instagram"></i></a> <a href=""><i class="fa fa-pinterest"></i></a> <a href=""><i class="fa fa-heart"></i></a> <a href=""><i class="fa fa-google-plus"></i></a> <a href=""><i class="fa fa-tumblr"></i></a>
  </ul>
</div>
<!-- social-icons -->

</div>
<!-- container -->
</nav>
<!--/.nav-collapse --> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

Try_Cast也会处理无效数据。如果传递一些无效数据,它将返回NULL。

答案 2 :(得分:1)

一样简单:

SELECT CONVERT(DATETIME,entrydate)

答案 3 :(得分:1)

试试这个,希望这对你有用。

enter image description here

答案 4 :(得分:0)

    ;WITH tb(entrydate)AS(
       SELECT '20161231' UNION
       SELECT '20161230' UNION
       SELECT '20161229' UNION
       SELECT '20161228'
    )
    SELECT CONVERT(DATETIME, STUFF(STUFF(tb.entrydate,5,0,'/'),8,0,'/')) FROM tb
-----------------------
2016-12-28 00:00:00.000
2016-12-29 00:00:00.000
2016-12-30 00:00:00.000
2016-12-31 00:00:00.000

答案 5 :(得分:0)

试试这个......

SELECT CAST(entrydate AS DATETIME) AS entrydate FROM tablename;