SQLSERVER 一些经典问题总结
SQLSERVER--一些经典问题总结
2007-04-01 01:04:06
大中小
/**********************************/
--获得某一天所在年的第一天
declare @a datetime,@b datetime,@sum int,@num int,@res varchar(20)
select @a='1-6-1968'
select @b='2006-4-8'
select @sum=year(@a)
select @res=ltrim(cast(@sum as varchar(20)))+'-1-1'
print @res
/*********************************/
--获得某一天所在月的最后一天
declare @a datetime,@b int,@c int,@res varchar(20)
select @a='2569-8-31'
select @b=month(dateadd(month,1,@a))
select @c=year(@a)
select @res=dateadd(day,-1,cast(@c as varchar(5))+'-'+cast(@b as varchar(20))+'-1')
print @res
/*********************************/
--获得某一天所在年的最后一天
declare @a datetime,@b int,@res varchar(20)
select @a='2026-1-5'
select @b=year(@a)+1
select @res=dateadd(day,-1,cast(@b as varchar(20))+'-1-1')
print @res
/*********************************/
/*返回当前年的最后一天*/
select dateadd(year,datediff(year,'1900-12-31',getdate()),'1900-12-31')
/*返回当月的最后一天*/
declare @a int,@b int,@c varchar(123)
select @a=datepart(year,dateadd(month,1,getdate()))
select @b=datepart(month,dateadd(month,1,getdate()))
select @c=str(@a)+'-'+ltrim(str(@b))+'-1'
print dateadd(day,datediff(day,'1900-1-1',@c)-1,'1900-1-1')
/*打印所有字母*/
if exists (select name from sysobjects where name='ff' and type='fn')
drop function ff
go
create function ff(@a varchar(20),@b int)
returns varchar(20)
as
begin
declare @res varchar(20)
select @res=substring(@a,@b,1)
return @res
end
--先写个函数获得对应位的字符,再打印
declare @a varchar(20),@i int,@len int
select @a='asdfgh'
select @len=len(@a)
select @i=1
while (@i<@len)
begin
print dbo.ff(@a,@i)
select @i=@i+1
end
/******************************************************************/
/******************************************************************************/
/******************************************************************************/
相关文档:
SQLServer中有两个扩展存储过程实现Scanf和Printf功能,恰当的使用它们可以在提取和拼接字符串时大幅度简化SQL代码。
1、xp_sscanf,用它可以分解格式相对固定的字符串,这对于厌倦使用一堆substring和charindex的朋友来说不错。比如前几天的一个帖子中提出的如何分解ip地址,相对简练且通用的代码应该是下面这样
------- ......
E盘根目录新建一个Excel文件aa.xls后测试如下代码
use tempdb
go
if (object_id ('udf_getExcelTableNames' ) is not null )
drop function dbo .udf_getExcelTableNames
go
create function udf_getExcelTableNames (@filename varchar (1000 ))
returns @t table (id int , name varchar ( ......
最近整理出来的.如果不完全的话希望大家补充.
在access中,转换为大写的sql函数是ucase,在sqlserver中,转换为大写的函数是upper;在access中,转换为小写的函数是lcase,在sqlserver中,转换为小写的函数是lower;在access中,取当前时间的函数是now,另外还有一个取日期函数date,在sqlserver中,取当前的函数是getdate ......
SQLServer 中含自增主键的表,通常不能直接指定ID值插入,可以采用以下方法插入。
1. SQLServer 自增主键创建语法:
identity(seed, increment)
其中
seed 起始值
increment 增量
示例:
create table student(
id int identity(1,1),
name varcha ......
临近年终,在工作之余对工作和学习中遇到的问题以及常用的一些知识点做了些整理,以备后用。本文涉及的内容为数据库,算是对开发总结(1)---数据库一文的补充。
1 对于主键设置了Identity的表,在删除表中数据后再往表中插入数据,Identity列不是从1起始了,如果想删除数据后Indentity列仍从1起始,可以用下面代码来删除数据 ......