当前位置:首页 » 编程语言 » 用sql怎么写函数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

用sql怎么写函数

发布时间: 2022-06-21 19:44:10

A. 使用sql编写一个函数

CREATE PROC Get_Sumdays
@YYMMDD1 datetime,@YYMMDD2 datetime
AS
BEGIN
Declare @Days int
select @Days=datediff(week,@YYMMDD1,@YYMMDD2) +(case when datepart(weekday,@YYMMDD1)>1 then 0 else 1 end)-(case when datepart(weekday,@YYMMDD2)>=1 then 0 else 1 end)
PRINT @Days
END

执行存储过程:Get_Sumdays '2009-08-01','2009-08-30'
@YYMMDD1 是开始日期 @YYMMDD2是结束日期

B. 以我下面的SQL语句!关于函数怎么写

create proc mypr1
@pe int
As
select a.name name1,a.*,b.* from piwik_site a,piwik_archive_numeric_2013_10 b where a.idsite=b.idsite and b.name='nb_visits' and period=@pe order by b.value desc

C. 用sql写函数,给定一个日期返回其为该年的第几天

#include<stdio.h>int
main(void){
int
year,month,day;
int
day_of_year(int
year,int
month,int
day);
while(scanf("%d%d%d",&year,&month,&day)!=eof){
printf("%d",day_of_year(year,month,day));
}
return
0;}
int
day_of_year(int
year,int
month,int
day){
int
k,leap,d=day;
//d没初始化
int
tab[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
//二维数组初始化不正确
leap=(year%4==0&&year%100!=0||year%400==0);
for(k=1;k<month;k++)
//逻辑错误,当前月只有day天,配合d=day,这里要去掉当前月
d=d+tab[leap][k];
return
d;}int
day_of_year2(int
year,int
month,int
day)
//稍微简化一下{
int
k,leap,d=day;
int
tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};
leap=(year%4==0&&year%100!=0||year%400==0);
for(k=0;k<month-1;k++)
d=d+tab[k];
if(leap==1&&month>2)d++;
//闰年并且输入月份大于2,加1天
return
d;}

D. 自己编写sql函数

create function [dbo].[Get_StrArrayStrOfIndex]
(
@str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int

set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)

set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。

return substring(@str,@start,@location-@start)
end

使用举例:
select id,code=dbo.Get_StrArrayStrOfIndex(code,'.',4) from code_table

E. 在线等,求SQL自定义函数写法

create function functionname
@part [类型],
@part [类型]
as
--[操作,计算]
return 1--[函数输出值]
---只允许查数据和计算,不可以有增、删、改的操作

select dbo.goods(商品信息.销售价格,进货单.进货单价,'2011-1-1',GETDATE,())
from 商品信息 inner join 进货单
on 商品信息.商品编号=进货单.商品编号

F. SQL中如何调用一个函数啊

1、首先是找到标量函数,右键单击选择新建。

G. sql怎么样写自定函数的格式例如newid()格式一样

SQL里面有个funcation
就是函数
create function
test(@Num
varchar(20))--@Num
参数,你这里应该就不用了,粗略看了下觉得是个随机七星彩之类的玩意。
returns
varchar(50)
--返回值类型
as
begin
declare
@table
table
(
c1
varchar(10)
)
declare
@i
int
set
@i=0
while
(@i<=9)
begin
insert
into
@table
select
@i
set
@i=@i+1
end
declare
@s
varchar(10)
set
@s
=2
select
@s
=@s+c1
from
(select
top
7
c1
from
@table
order
by
newid())
T
select
@s
return
@s
end
--调用函数
select
dbo.test()--传递参数

而即使上因为你没有穿入参数,存储过程这些也能做这些事

H. sql中如何写函数

/*
获取两个数值的大小
0:小于;1:大于;2:等于
*/
CREATE function GetMax(@qty1 varchar(50),@qty2 varchar(50))
returns int
begin
declare @Num int
if (convert(decimal(18,5),@qty1)-convert(decimal(18,5),@qty2) >0)
begin
set @Num=1
end
else if (convert(decimal(18,5),@qty1)-convert(decimal(18,5),@qty2) <0)
begin
set @Num=0
end
else
begin
set @Num=2
end

return @Num
end

I. 0的数字型函数sql怎么写

当COUNT(B)的结果为0时,恰好与第二个给定的参数0相等,这个时候NULLIF函数就会返回NULL,而SUM(A)在除以NULL时结果为NULL,外层使用ISNULL函数再对NULL值进行判断,这样最终结果就是0了。
SQL是一种具有数据操纵,数据定义等多种功能的数据库语言。SQL具有交互性特点,能为用户提供极大的便利,利用SQL语言,可以提高计算机应用系统的工作质量与效率。
结构化查询语言简称SQL。结构化查询语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统,可以使用相同的结构化查询语言作为数据输入与管理的接口。

J. sql函数的编写格式是怎样的

函数的格式如下:
如下函数名为inttochar(),使用时这样:select dbo.inttochar(1234,10)

CREATE FUNCTION [dbo].[inttochar] (@int int=0, @length int=1)
RETURNS varchar(250) AS
--将@int转换成长度为@length的字符串,长度不足时加前导0
BEGIN
declare @tmp varchar(250)
select @tmp=cast(@int as varchar(250))
while len(@tmp)<@length begin
select @tmp='0'+@tmp
end
return @tmp
END