---
title: "convert和cast"
author: "神奇九寨沟"
date: "2008-11-07T17:31:22Z"
tags: []
slug: "convert-cast"
draft: false
lastmod: "2008-11-07T17:31:22Z"
id: 264133
url: "https://webwork123.iteye.com/blog/264133"
---

<span style="color:red">------------convert日期转成字符串</span>select getdate() 
select <span style="color:red">convert</span>(char(8),getdate(),112) 
select convert(char(8),getdate(),108) 

<span style="color:red">--可以得到日期的部分值，</span>
select year(getdate())    
select datepart(yy,getdate())
select datepart(mm,getdate())


<span style="color:red">--这样就可以得到两个年份的相差值
------字符串转成日期</span>select year(getdate())-datepart(yy,<span style="color:red">cast</span>('1983-12-31' as datetime))