网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > 数据库 > SQL技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
数据库:数据库教程,数据库技巧,Oracle教程,MySQL教程,Sybase教程,Access教程,DB2教程,数据库安全,数据库文摘
本月文章推荐
.用存储过程几个最直接的好处.
.SQL IF..ELSE..在存储过程的使用.
.讲解SQL Server 2005数据库的同义.
.SQL Prompt:SQL智能完成工具!.
.在IE中直接连接SQL数据库 .
.巧用数据库引擎优化Tuning Advis.
.SQL Server数据库简体繁体数据混.
.在SQL Server 2005中解决死锁.
.确保所有的非空(Non-NULL)值都.
.SQL Server根据查询结果,生成XML.
.由浅入深讲解SQL Server 2005数据.
.MS SQL SERVER 图像或大文本的输.
.讲解SQL Server 2005数据库表的递.
.win98+PWS环境下连接读取远程SQL.
.Sql server数据库备份还原另一方.
.Sql Server 2000数据库日志日益庞.
.精彩的近乎完美的分页存储过程.
.教你快速掌握数据库设计的五种常.
.TEMP表空间肆意扩大无法继续扩展.
.调整SQLServer2000运行中数据库结.

循环插入一定时间范围内的随机日期值

发表日期:2008-4-1


SQL Server中循环插入一定时间范围内的随机日期值

-- First, let's declare the date range. I am declaring this
     -- here for the demo, but this could be done anyway you like.
     DECLARE @date_from DATETIME;
     DECLARE @date_to DATETIME;
      
     -- Set the start and date dates. In this case, we are using
     -- the month of october, 2006.
     SET @date_from = '2006-10-01';
     SET @date_to = '2006-10-30';
      
     -- insert  random dates.
     insert into randomdate SELECT
     (
     -- Remember, we want to add a random number to the
     -- start date. In SQL we can add days (as integers)
     -- to a date to increase the actually date/time
     -- object value.
     @date_from +
     (
     -- This will force our random number to be GTE 0.
     ABS(
      
     -- This will give us a HUGE random number that
     -- might be negative or positive.
     CAST(
     CAST( NewID() AS BINARY(8) )
     AS INT
     )
     )
      
     -- Our random number might be HUGE. We can't have
     -- exceed the date range that we are given.
     -- Therefore, we have to take the modulus of the
     -- date range difference. This will give us between
     -- zero and one less than the date range.
     %
      
     -- To get the number of days in the date range, we
     -- can simply substrate the start date from the
     -- end date. At this point though, we have to cast
     -- to INT as SQL will not make any automatic
     -- conversions for us.
     CAST(
     (@date_to - @date_from)
     AS INT
     )
     )
     )
上一篇:SQL Server 2005中三种插入XML数据的方法 人气:992
下一篇:SQL语句应当如何使用内嵌视图与临时表 人气:1214
浏览全部SQL Server的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐