June 29, 2010 at 7:09 pm
WayneS (6/29/2010)
vince.chittenden (6/29/2010)
If the month function never returns a zero-length string (and it never will in this case, AFAICS), then why not '0' instead of '00'?You are correct, this would work.
I just got into the habit years ago that to ensure I had two characters, to always specify two.
I'm also in the same habit. It also helps a bit with "understanding" that there will be two characters always. It only costs 1 extra character, virtually no extra time even on a million rows, and makes the code more "bullet proof" (not in this case, but in most).
--Jeff Moden
Change is inevitable... Change for the better is not.
June 29, 2010 at 8:07 pm
Agile (6/28/2010)
Thanks wayne for the suggestion, Im just wondering, Because when I use your solution, I did not declare variable for Date. Is there a difference in performance on doing that? Or just for the purpose of readability of the codes?
declare @fn varchar(100),
@cmd varchar(200)
set @fn = 'E:\' +
right('00' + convert(varchar(2), month(DateAdd(day, -1, getdate()))), 2) +
right('00' + convert(varchar(2), day(DateAdd(day, -1, getdate()))),2) +
convert(char(4), year(DateAdd(day, -1, getdate()))) + '.Log'
SELECT @cmd = 'bcp "##LogWithNightShift" out ' + @fn + ' -f -fbcp.fmt -c -P"."'
print @cmd
--EXEC master..xp_cmdshell @cmd
You can simplify that code a bit using the date formatting in the convert function:
declare @cmd varchar(200)
select @cmd =
'bcp "##LogWithNightShift" out E:\' +
replace(convert(varchar(10),getdate()-1,101),'/','')+
'.Log -f -fbcp.fmt -c -P"."'
print @cmd
--EXEC master..xp_cmdshell @cmd
Results:
bcp "##LogWithNightShift" out E:\06282010.Log -f -fbcp.fmt -c -P"."
June 30, 2010 at 7:11 am
As a sidebar, I'd use format 112 so the file names are sortable.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 30, 2010 at 6:24 pm
Thanks michael,yeah your right this one is more simple. And thanks to everyone.
declare @cmd varchar(200)
select @cmd =
'bcp "##LogWithNightShift" out E:\' +
replace(convert(varchar(10),getdate()-1,101),'/','')+
'.Log -f -fbcp.fmt -c -P"."'
print @cmd
--EXEC master..xp_cmdshell @cmd
Thanks,
Morris
Viewing 4 posts - 16 through 18 (of 18 total)
You must be logged in to reply to this topic. Login to reply