SQL函数-出票日期的规范写法

支票的日期填写方法:逢10日,20日,30日,都要在前面加零,如零壹拾日,零贰拾日,零叁拾日,因为不这样写,这些日子后面还可以填数字,有可能造成人为漏洞,而导致支票无法兑付,银行也会提醒客户补填或直接退票。

以下通过SQL2000自定义函数实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
if  exists(select name from sysobjects where name ='f_DateOfChinese' )
   drop function f_DateOfChinese;
go
create function f_DateOfChinese(@date datetime)
returns varchar(40)
as
begin
-- 环境: sql2000
-- 《经济法》出票日期的规范写法
-- charset= GB3212
-- 2021年2月10日;转为:贰零贰壹年零贰月零壹拾日
declare @year int;
declare @yearStr varchar(10);
declare @month int;
declare @monthStr varchar(10);
declare @day int;
declare @dayStr varchar(10);
declare @arraylist varchar(20);
declare @arraylist2 varchar(20);
declare @i int;
declare @m int;
declare @d int;
declare @day1 int;
declare @len int;
declare @num int;
declare @word varchar(10);
declare @getdate datetime;
begin
  set @getdate = @date ; -- '2021-10-31';
  set @arraylist ='零壹贰叁肆伍陆柒捌玖';
  SET @arraylist2='整拾佰仟万拾佰仟亿拾佰仟';
  set @year = year(@getdate);
  set @i= 1;
  set @yearStr = '';
  set @word='';
 
  -- 1): 转换年份
  while @i<=4
  begin
    set @num = cast( substring(cast(@year as varchar(10)),@i,1) as int);
   -- print str(@num)
    set @word = substring(@arraylist,@num+1,1);
    set @yearStr = @yearStr +@word;
    set @i = @i + 1;
  end;
  SET @yearStr = @yearStr+'年';
 -- print @yearStr;
 
  set @month = month(@getdate);
 -- print str(@month);
  --- 2)处理月份
  set @monthStr = '';
  set @m=1;
  SELECT @MONTHSTR= (CASE WHEN
      2< @MONTH AND @month<10 THEN 
     substring(@arraylist,@month+1,1)+'月'
     WHEN @MONTH<=2 THEN
       '零'substring(@arraylist,@month+1,1)+'月'
     WHEN
       @MONTH=10  THEN 
    '零壹拾月'
    WHEN
       @MONTH=11  THEN 
    '壹拾壹月'
   WHEN
       @MONTH=12  THEN 
    '壹拾贰月'
   -- set @m = @m+1;
    ELSE
      ''
  END) ;
  --print @yearStr+@monthStr;
 
  -- 3):处理"日"的部分
  set @day = day(@getdate);
  set @len = len(@day);
  set @d = 1;
  set @dayStr = '日';
  while @d <= @len
  begin
    set @day1 = cast( substring( cast(@day as varchar(4)),@len-@d+1,1) as varchar(10));
   -- print str(@day1);
    select @dayStr =( substring(@arraylist,@day1+1,1))+ ( substring(@arraylist2,@d,1))+@dayStr;
    set @d = @d+1;
  end;
  set @dayStr = replace(@dayStr,'整','');
  set @dayStr = replace(@dayStr,'零','');
 -- print replace(@dayStr,'整','');
 -- print replace(@dayStr,'零','');
   
  if @day<10
   set @dayStr = '零'+ @dayStr;
  else if (@day%10 =0 )  --余数,求模 mod()
    set @dayStr=  '零'+ @dayStr;
  else
    set @dayStr = @dayStr;
 
  
end;
  return( @yearStr + @monthStr + @dayStr);
end
 
 
 
go
--
 select dbo.f_DateOfChinese( '2020-10-20') as dateofChinese;
--贰零贰零年零壹拾月零贰拾日
-- 贰零贰零年壹拾贰月零贰拾日 

  

透过程序化理解“出票日期的规范写法”。

posted @   samrv  阅读(348)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示