SQLServer各种日期计算方法
SQL是高级的非过程化编程语言,一般针对数据库进行操作。
定义:datediff(day/month/year,startdate,enddate)
日期函数:(要返回的天数/月数/年数,开始日期,结束日期)
具体形式:select 1,2,datediff(day,1,2) 天数 from aa
还有一些方法:
使用DATEDIFF函数可以求两个日期之间相差的天数。
MySQL中的DATEDIFF函数仅需要两个参数 (即要计算相差天数的两个日期),第一个参数应是两个日期中较小的值,以避免出现负值(SQL Server中正好相反)。
在SQL Server中,可以指定该函数返回值所表示的类型(在这个例子中,返回以“日”为单位的差)。下面的解决方案采用了SQL Server的版本:
1 select datediff(day,allen_hd,ward_hd)
2 from (
3 select hiredate as ward_hd
4 from emp
5 where ename = 'WARD'
6 ) x,
7 (
8 select hiredate as allen_hd
9 from emp
10 where ename = 'ALLEN'
11 ) y
MySQL用户只需去掉该函数的第一个参数,交换一下传递ALLEN_HD和WARD_HD的顺序即可。
你可以先将获取的年月日拼接成“正确”的日期格式eg:年 月 日2013 03 25这样用/将年月日隔开,拼接之后就成了一个字符串:“2013/03/25”。在用Convert.ToDateTime的方式转。或者format的方式
如何在mysql数据表中显示当前时间
1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp() current_timestamp localtime() localtime localtimestamp() localtimestamp 这些日期时间函数,都等同于 now()。
鉴于 now() 函数简短易记,建议总是使用 now() 来替代上面列出的函数。1.2 获得当前日期+时间(date + time)函数:sysdate() sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。2. 获得当前日期(date)函数:curdate() 其中,下面的两个日期函数等同于 curdate(): current_date(),current_date 3. 获得当前时间(time)函数:curtime() 其中,下面的两个时间函数等同于 curtime():current_time(),current_time 4. 获得当前 UTC 日期时间函数:utc_date(), utc_time(), utc_timestamp()
还没有评论,来说两句吧...