mssql数据库字符串格式化问题

china0tech 2013-07-24 10:30:03
现有SN表如下:
SNID SNFormat
1 "CK" - YYYY - MM - DD - [000]
2 "WWL" - YYYY - MM - DD - [000]
3 "WWT" - YYYY - MM - DD - [000]
4 "SK" - yyyy - mm - dd - [000]
5 "DF" - yyyy - mm - dd - [000]
6 "JT" - yyyy - mm - dd - [000]
7 "JHD" - yyyy - mm - dd - [000]

Bill表如下:
SNID MaxNo
1 5
2 4
3 6
4 111
5 0
6 1
7 20

现在需求如下:我传入SNID和日期返回一个格式化后的的值。
比如:我传入SNID=1,日期=2013-07-24,
那么返回的值应该为CK-2013-MM-DD-006
我传入snid=4,日期=2013-07-24,
那么返回的值应该为SK-yyyy-mm-dd-112

还望DBA帮帮忙,先谢了。

...全文
223 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
china0tech 2013-07-24
  • 打赏
  • 举报
回复
引用 10 楼 wmh178 的回复:
[quote=引用 9 楼 china0tech 的回复:] [quote=引用 8 楼 wmh178 的回复:]

Declare @ID int
Declare @Adate datetime

set @ID=1
Set @Adate='2013-07-24'


Declare @table1 table (SNID int ,SNFormat varchar(50))
Declare @table2	table(SNID int, MaxNo int)
insert @table1
select	 1,N'"CK" - YYYY - MM - DD - [000]'	union all
select	 2,N'"WWL" - YYYY - MM - DD - [000]'	union all
select	 3,N'"WWT" - YYYY - MM - DD - [000]'	union all
select	 4,N'"SK" - yyyy - mm - dd - [000]'	union all
select	 5,N'"DF" - yyyy - mm - dd - [000]'	union all
select	 6,N'"JT" - yyyy - mm - dd - [000]'	union all
select	 7,N'"JHD" - yyyy - mm - dd - [000]'
insert @table2
select 1,             5		union all
select 2,             4		union all
select 3,             6		union all
select 4,             111	union all
select 5,             0		union all
select 6,             1		union all
select 7,             20



select t1.SNID,t2.MaxNo,t1.SNFormat
	,AfterReplace=Replace(Replace(t1.SNFormat,' ',''),'"','')
	,FormatStr= Replace(Replace(Replace(Replace(t1.SNFormat,' ',''),'"',''),'yyyy-mm-dd',convert(char(10),@Adate,120)),'[000]',right('000'+cast(t2.MaxNo+1 as varchar),3))
from @table1 t1
		join @table2 t2 on t1.SNID= t2.SNID
--where t1.SNID=@ID 

/*
SNID	MaxNo	SNFormat	AfterReplace	FormatStr
1	5	"CK" - YYYY - MM - DD - [000]	CK-YYYY-MM-DD-[000]	CK-2013-07-24-006
2	4	"WWL" - YYYY - MM - DD - [000]	WWL-YYYY-MM-DD-[000]	WWL-2013-07-24-005
3	6	"WWT" - YYYY - MM - DD - [000]	WWT-YYYY-MM-DD-[000]	WWT-2013-07-24-007
4	111	"SK" - yyyy - mm - dd - [000]	SK-yyyy-mm-dd-[000]	SK-2013-07-24-112
5	0	"DF" - yyyy - mm - dd - [000]	DF-yyyy-mm-dd-[000]	DF-2013-07-24-001
6	1	"JT" - yyyy - mm - dd - [000]	JT-yyyy-mm-dd-[000]	JT-2013-07-24-002
7	20	"JHD" - yyyy - mm - dd - [000]	JHD-yyyy-mm-dd-[000]	JHD-2013-07-24-021
*/ 
嗯,这个可以满足需求。但是如果我SNFormat的格式变成SK-mm-dd-[000000],我需要得到的值是SK-07-24-[000112],有什么办法能让convert灵活转换呢?[/quote] 老大 无非就那几个函数 你可以根据自己的需要进行改变 按照你说的 最好放到存储过程里面 根据传过来的参数类型不同 就给他返回相应的字符串 自己多动手才能进步 才能掌握[/quote] 嗯,结贴吧。辛苦你了。
偶麦嘎 2013-07-24
  • 打赏
  • 举报
回复
引用 9 楼 china0tech 的回复:
[quote=引用 8 楼 wmh178 的回复:]

Declare @ID int
Declare @Adate datetime

set @ID=1
Set @Adate='2013-07-24'


Declare @table1 table (SNID int ,SNFormat varchar(50))
Declare @table2	table(SNID int, MaxNo int)
insert @table1
select	 1,N'"CK" - YYYY - MM - DD - [000]'	union all
select	 2,N'"WWL" - YYYY - MM - DD - [000]'	union all
select	 3,N'"WWT" - YYYY - MM - DD - [000]'	union all
select	 4,N'"SK" - yyyy - mm - dd - [000]'	union all
select	 5,N'"DF" - yyyy - mm - dd - [000]'	union all
select	 6,N'"JT" - yyyy - mm - dd - [000]'	union all
select	 7,N'"JHD" - yyyy - mm - dd - [000]'
insert @table2
select 1,             5		union all
select 2,             4		union all
select 3,             6		union all
select 4,             111	union all
select 5,             0		union all
select 6,             1		union all
select 7,             20



select t1.SNID,t2.MaxNo,t1.SNFormat
	,AfterReplace=Replace(Replace(t1.SNFormat,' ',''),'"','')
	,FormatStr= Replace(Replace(Replace(Replace(t1.SNFormat,' ',''),'"',''),'yyyy-mm-dd',convert(char(10),@Adate,120)),'[000]',right('000'+cast(t2.MaxNo+1 as varchar),3))
from @table1 t1
		join @table2 t2 on t1.SNID= t2.SNID
--where t1.SNID=@ID 

/*
SNID	MaxNo	SNFormat	AfterReplace	FormatStr
1	5	"CK" - YYYY - MM - DD - [000]	CK-YYYY-MM-DD-[000]	CK-2013-07-24-006
2	4	"WWL" - YYYY - MM - DD - [000]	WWL-YYYY-MM-DD-[000]	WWL-2013-07-24-005
3	6	"WWT" - YYYY - MM - DD - [000]	WWT-YYYY-MM-DD-[000]	WWT-2013-07-24-007
4	111	"SK" - yyyy - mm - dd - [000]	SK-yyyy-mm-dd-[000]	SK-2013-07-24-112
5	0	"DF" - yyyy - mm - dd - [000]	DF-yyyy-mm-dd-[000]	DF-2013-07-24-001
6	1	"JT" - yyyy - mm - dd - [000]	JT-yyyy-mm-dd-[000]	JT-2013-07-24-002
7	20	"JHD" - yyyy - mm - dd - [000]	JHD-yyyy-mm-dd-[000]	JHD-2013-07-24-021
*/ 
嗯,这个可以满足需求。但是如果我SNFormat的格式变成SK-mm-dd-[000000],我需要得到的值是SK-07-24-[000112],有什么办法能让convert灵活转换呢?[/quote] 老大 无非就那几个函数 你可以根据自己的需要进行改变 按照你说的 最好放到存储过程里面 根据传过来的参数类型不同 就给他返回相应的字符串 自己多动手才能进步 才能掌握
china0tech 2013-07-24
  • 打赏
  • 举报
回复
引用 8 楼 wmh178 的回复:

Declare @ID int
Declare @Adate datetime

set @ID=1
Set @Adate='2013-07-24'


Declare @table1 table (SNID int ,SNFormat varchar(50))
Declare @table2	table(SNID int, MaxNo int)
insert @table1
select	 1,N'"CK" - YYYY - MM - DD - [000]'	union all
select	 2,N'"WWL" - YYYY - MM - DD - [000]'	union all
select	 3,N'"WWT" - YYYY - MM - DD - [000]'	union all
select	 4,N'"SK" - yyyy - mm - dd - [000]'	union all
select	 5,N'"DF" - yyyy - mm - dd - [000]'	union all
select	 6,N'"JT" - yyyy - mm - dd - [000]'	union all
select	 7,N'"JHD" - yyyy - mm - dd - [000]'
insert @table2
select 1,             5		union all
select 2,             4		union all
select 3,             6		union all
select 4,             111	union all
select 5,             0		union all
select 6,             1		union all
select 7,             20



select t1.SNID,t2.MaxNo,t1.SNFormat
	,AfterReplace=Replace(Replace(t1.SNFormat,' ',''),'"','')
	,FormatStr= Replace(Replace(Replace(Replace(t1.SNFormat,' ',''),'"',''),'yyyy-mm-dd',convert(char(10),@Adate,120)),'[000]',right('000'+cast(t2.MaxNo+1 as varchar),3))
from @table1 t1
		join @table2 t2 on t1.SNID= t2.SNID
--where t1.SNID=@ID 

/*
SNID	MaxNo	SNFormat	AfterReplace	FormatStr
1	5	"CK" - YYYY - MM - DD - [000]	CK-YYYY-MM-DD-[000]	CK-2013-07-24-006
2	4	"WWL" - YYYY - MM - DD - [000]	WWL-YYYY-MM-DD-[000]	WWL-2013-07-24-005
3	6	"WWT" - YYYY - MM - DD - [000]	WWT-YYYY-MM-DD-[000]	WWT-2013-07-24-007
4	111	"SK" - yyyy - mm - dd - [000]	SK-yyyy-mm-dd-[000]	SK-2013-07-24-112
5	0	"DF" - yyyy - mm - dd - [000]	DF-yyyy-mm-dd-[000]	DF-2013-07-24-001
6	1	"JT" - yyyy - mm - dd - [000]	JT-yyyy-mm-dd-[000]	JT-2013-07-24-002
7	20	"JHD" - yyyy - mm - dd - [000]	JHD-yyyy-mm-dd-[000]	JHD-2013-07-24-021
*/ 
嗯,这个可以满足需求。但是如果我SNFormat的格式变成SK-mm-dd-[000000],我需要得到的值是SK-07-24-[000112],有什么办法能让convert灵活转换呢?
偶麦嘎 2013-07-24
  • 打赏
  • 举报
回复

Declare @ID int
Declare @Adate datetime

set @ID=1
Set @Adate='2013-07-24'


Declare @table1 table (SNID int ,SNFormat varchar(50))
Declare @table2	table(SNID int, MaxNo int)
insert @table1
select	 1,N'"CK" - YYYY - MM - DD - [000]'	union all
select	 2,N'"WWL" - YYYY - MM - DD - [000]'	union all
select	 3,N'"WWT" - YYYY - MM - DD - [000]'	union all
select	 4,N'"SK" - yyyy - mm - dd - [000]'	union all
select	 5,N'"DF" - yyyy - mm - dd - [000]'	union all
select	 6,N'"JT" - yyyy - mm - dd - [000]'	union all
select	 7,N'"JHD" - yyyy - mm - dd - [000]'
insert @table2
select 1,             5		union all
select 2,             4		union all
select 3,             6		union all
select 4,             111	union all
select 5,             0		union all
select 6,             1		union all
select 7,             20



select t1.SNID,t2.MaxNo,t1.SNFormat
	,AfterReplace=Replace(Replace(t1.SNFormat,' ',''),'"','')
	,FormatStr= Replace(Replace(Replace(Replace(t1.SNFormat,' ',''),'"',''),'yyyy-mm-dd',convert(char(10),@Adate,120)),'[000]',right('000'+cast(t2.MaxNo+1 as varchar),3))
from @table1 t1
		join @table2 t2 on t1.SNID= t2.SNID
--where t1.SNID=@ID 

/*
SNID	MaxNo	SNFormat	AfterReplace	FormatStr
1	5	"CK" - YYYY - MM - DD - [000]	CK-YYYY-MM-DD-[000]	CK-2013-07-24-006
2	4	"WWL" - YYYY - MM - DD - [000]	WWL-YYYY-MM-DD-[000]	WWL-2013-07-24-005
3	6	"WWT" - YYYY - MM - DD - [000]	WWT-YYYY-MM-DD-[000]	WWT-2013-07-24-007
4	111	"SK" - yyyy - mm - dd - [000]	SK-yyyy-mm-dd-[000]	SK-2013-07-24-112
5	0	"DF" - yyyy - mm - dd - [000]	DF-yyyy-mm-dd-[000]	DF-2013-07-24-001
6	1	"JT" - yyyy - mm - dd - [000]	JT-yyyy-mm-dd-[000]	JT-2013-07-24-002
7	20	"JHD" - yyyy - mm - dd - [000]	JHD-yyyy-mm-dd-[000]	JHD-2013-07-24-021
*/ 
china0tech 2013-07-24
  • 打赏
  • 举报
回复
引用 6 楼 wmh178 的回复:
看看这个是不是你想要的

Declare @ID int
Declare @Adate datetime

set @ID=1
Set @Adate='2013-07-24'


Declare @table1 table (SNID int ,SNFormat varchar(50))
Declare @table2	table(SNID int, MaxNo int)
insert @table1
select 1,'CK-YYYY-MM-DD-[000]'	union all
select 2,'WWL-YYYY-MM-DD-[000]'	union all
select 3,'WWT-YYYY-MM-DD-[000]'	union all
select 4,'SK-yyyy-mm-dd-[000]'	union all
select 5,'DF-yyyy-mm-dd-[000]'	union all
select 6,'JT-yyyy-mm-dd-[000]'	union all
select 7,'JHD-yyyy-mm-dd-[000]'
insert @table2
select 1,             5		union all
select 2,             4		union all
select 3,             6		union all
select 4,             11	union all
select 5,             0		union all
select 6,             1		union all
select 7,             20


select FormatStr= Replace(Replace(t1.SNFormat ,'yyyy-mm-dd',convert(char(10),@Adate,120)),'[000]',right('000'+cast(t2.MaxNo+1 as varchar),3))
from @table1 t1
		join @table2 t2 on t1.SNID= t2.SNID
--where t1.SNID=@ID 

谢谢你的解答。主要是要能根式化日期。 建表脚本:

sql脚本如下:
[code=sql]
if exists (select * from sysobjects where id = OBJECT_ID('[Bill]') and OBJECTPROPERTY(id, 'IsUserTable') = 1) 
DROP TABLE [Bill]

CREATE TABLE [Bill] (
[SNID] [int]  NOT NULL,
[MaxNo] [int]  NULL)

INSERT [Bill] ([SNID],[MaxNo]) VALUES ( 1,5)
INSERT [Bill] ([SNID],[MaxNo]) VALUES ( 2,4)
INSERT [Bill] ([SNID],[MaxNo]) VALUES ( 3,6)
INSERT [Bill] ([SNID],[MaxNo]) VALUES ( 4,111)
INSERT [Bill] ([SNID],[MaxNo]) VALUES ( 5,0)
INSERT [Bill] ([SNID],[MaxNo]) VALUES ( 6,1)
INSERT [Bill] ([SNID],[MaxNo]) VALUES ( 7,20)


if exists (select * from sysobjects where id = OBJECT_ID('[SN]') and OBJECTPROPERTY(id, 'IsUserTable') = 1) 
DROP TABLE [SN]

CREATE TABLE [SN] (
[SNID] [int]  NOT NULL,
[SNFormat] [varchar]  (1000) NULL)

INSERT [SN] ([SNID],[SNFormat]) VALUES ( 1,N'"CK" - YYYY - MM - DD - [000]')
INSERT [SN] ([SNID],[SNFormat]) VALUES ( 2,N'"WWL" - YYYY - MM - DD - [000]')
INSERT [SN] ([SNID],[SNFormat]) VALUES ( 3,N'"WWT" - YYYY - MM - DD - [000]')
INSERT [SN] ([SNID],[SNFormat]) VALUES ( 4,N'"SK" - yyyy - mm - dd - [000]')
INSERT [SN] ([SNID],[SNFormat]) VALUES ( 5,N'"DF" - yyyy - mm - dd - [000]')
INSERT [SN] ([SNID],[SNFormat]) VALUES ( 6,N'"JT" - yyyy - mm - dd - [000]')
INSERT [SN] ([SNID],[SNFormat]) VALUES ( 7,N'"JHD" - yyyy - mm - dd - [000]')


你得到的数据是 "CK" - YYYY - MM - DD - 006 "WWL" - YYYY - MM - DD - 005 "WWT" - YYYY - MM - DD - 007 "SK" - yyyy - mm - dd - 112 "DF" - yyyy - mm - dd - 001 "JT" - yyyy - mm - dd - 002 "JHD" - yyyy - mm - dd - 021 我想要的是这个 CK-2013-07-24-006 WWL-2013-07-24-005 WWT-2013-07-24-007 SK-2013-07-24-112 DF-2013-07-24-001 JT-2013-07-24-002 JHD-2013-07-24-021 需要处理双引号,和日期 [/code]
偶麦嘎 2013-07-24
  • 打赏
  • 举报
回复
看看这个是不是你想要的

Declare @ID int
Declare @Adate datetime

set @ID=1
Set @Adate='2013-07-24'


Declare @table1 table (SNID int ,SNFormat varchar(50))
Declare @table2	table(SNID int, MaxNo int)
insert @table1
select 1,'CK-YYYY-MM-DD-[000]'	union all
select 2,'WWL-YYYY-MM-DD-[000]'	union all
select 3,'WWT-YYYY-MM-DD-[000]'	union all
select 4,'SK-yyyy-mm-dd-[000]'	union all
select 5,'DF-yyyy-mm-dd-[000]'	union all
select 6,'JT-yyyy-mm-dd-[000]'	union all
select 7,'JHD-yyyy-mm-dd-[000]'
insert @table2
select 1,             5		union all
select 2,             4		union all
select 3,             6		union all
select 4,             11	union all
select 5,             0		union all
select 6,             1		union all
select 7,             20


select FormatStr= Replace(Replace(t1.SNFormat ,'yyyy-mm-dd',convert(char(10),@Adate,120)),'[000]',right('000'+cast(t2.MaxNo+1 as varchar),3))
from @table1 t1
		join @table2 t2 on t1.SNID= t2.SNID
--where t1.SNID=@ID 

china0tech 2013-07-24
  • 打赏
  • 举报
回复
不好意思,1楼的需求有点错误。 晕死,刚才写好的。我写错了,不能编辑修改吗? 现有SN表如下: SNID SNFormat 1 "CK" - YYYY - MM - DD - [000] 2 "WWL" - YYYY - MM - DD - [000] 3 "WWT" - YYYY - MM - DD - [000] 4 "SK" - yyyy - mm - dd - [000] 5 "DF" - yyyy - mm - dd - [000] 6 "JT" - yyyy - mm - dd - [000] 7 "JHD" - yyyy - mm - dd - [000] Bill表如下: SNID MaxNo 1 5 2 4 3 6 4 111 5 0 6 1 7 20 现在需求如下:我传入SNID和日期返回一个格式化后的的值。 比如:我传入SNID=1,日期=2013-07-24, 那么返回的值应该为CK-2013-07-27-006 我传入snid=4,日期=2013-07-24, 那么返回的值应该为SK-2013-07-24-112
china0tech 2013-07-24
  • 打赏
  • 举报
回复
引用 2 楼 wmh178 的回复:
[quote=引用 1 楼 zbdzjx 的回复:] 没看懂。 SNID=1时,为什么是2013,后面是006。 SNID=4时,为什么是yyyy,后面是112。
最大数(MaxNo+1) 目测应该是 自动生成某个流水单号[/quote] 嗯,是生成单据的问题
china0tech 2013-07-24
  • 打赏
  • 举报
回复
引用 1 楼 zbdzjx 的回复:
没看懂。 SNID=1时,为什么是2013,后面是006。 SNID=4时,为什么是yyyy,后面是112。
晕死,刚才写好的。我写错了,不能编辑修改吗? 现有SN表如下: SNID SNFormat 1 "CK" - YYYY - MM - DD - [000] 2 "WWL" - YYYY - MM - DD - [000] 3 "WWT" - YYYY - MM - DD - [000] 4 "SK" - yyyy - mm - dd - [000] 5 "DF" - yyyy - mm - dd - [000] 6 "JT" - yyyy - mm - dd - [000] 7 "JHD" - yyyy - mm - dd - [000] Bill表如下: SNID MaxNo 1 5 2 4 3 6 4 111 5 0 6 1 7 20 现在需求如下:我传入SNID和日期返回一个格式化后的的值。 比如:我传入SNID=1,日期=2013-07-24, 那么返回的值应该为CK-2013-07-27-006 我传入snid=4,日期=2013-07-24, 那么返回的值应该为SK-2013-07-24-112
偶麦嘎 2013-07-24
  • 打赏
  • 举报
回复
引用 1 楼 zbdzjx 的回复:
没看懂。 SNID=1时,为什么是2013,后面是006。 SNID=4时,为什么是yyyy,后面是112。
最大数(MaxNo+1) 目测应该是 自动生成某个流水单号
zbdzjx 2013-07-24
  • 打赏
  • 举报
回复
没看懂。 SNID=1时,为什么是2013,后面是006。 SNID=4时,为什么是yyyy,后面是112。

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧