November 28, 2008 at 7:57 am
When I pull the dateparts for minutes and seconds and concatenate, everything is fine. However, when I concatenate hours with minutes there is a space in between them. Can someone PLEASE explain?
DECLARE
@RECORDCOUNT varchar(7),
@CHEKSUMRCDS int,
@CHEKSUMIRNS int,
@RECORDSTATUS Char(1),
@SORTTYPE Char(2),
@RECORDFORMATINDICATOR char(1),
@FISCALYEAR Char(4),
@Now datetime,
@REPORTINGPERIOD Char(1),
@DISTRICTIRN Char(6)
,@strIRNCkSm varchar(10)
SELECT
@Now = getdate()
SELECT
-- two digit hour
replicate('0', 2 - len(convert(char(2),datepart(hh,@Now)))) + convert(char(2),datepart(hh,@Now))
-- two digit minute
, replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now))
-- length of two digit hour
, len(replicate('0', 2 - len(convert(char(2),datepart(hh,@Now)))) + convert(char(2),datepart(hh,@Now)))
-- length of two digit minute
, len( replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now)) )
-- concatenate hour and minute
, replicate('0', 2 - len(convert(char(2),datepart(hh,@Now)))) + convert(char(2),datepart(hh,@Now))
+ replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now))
-- length of contcatenated hour and minute
, len(replicate('0', 2 - len(convert(char(2),datepart(hh,@Now)))) + convert(char(2),datepart(hh,@Now))
+ replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now)) )
TIA :hehe:
November 28, 2008 at 8:16 am
Not sure what your question is. When I run your strings, I get this:
10 142210144
What are you getting?
November 28, 2008 at 8:20 am
09 45 2 2 09 45 5
is what I am getting.
November 28, 2008 at 8:50 am
Interesting. Not sure what local settings would cause something like that. You could probably get rid of it with a REPLACE(x,' ',''), but that seems a bit wasteful.
November 28, 2008 at 8:57 am
I have replaced the whole concatenation thing with the following very simple code:
replace(convert(char(8), @Now, 108), ':', '') AS FileTime
BUT... the anamoly is driving me nuts none the less. I was hoping I had overlooked something and someone could point it out to me.
π
November 28, 2008 at 9:05 am
Sue (11/28/2008)
09 45 2 2 09 45 5is what I am getting.
It falls over with single-digit times. Try casting the hours portion to VARCHAR instead:
SELECT
@Now = '2008-11-28 09:45:57.783' --getdate()
SELECT
-- two digit hour
replicate('0', 2 - len(convert(char(2),datepart(hh,@Now)))) + convert(char(2),datepart(hh,@Now))
-- two digit minute
, replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now))
-- length of two digit hour
, len(replicate('0', 2 - len(convert(char(2),datepart(hh,@Now)))) + convert(char(2),datepart(hh,@Now)))
-- length of two digit minute
, len( replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now)) )
-- concatenate hour and minute
, replicate('0', 2 - len(convert(VARchar(2),datepart(hh,@Now)))) + convert(VARchar(2),datepart(hh,@Now))
+ replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now))
-- length of contcatenated hour and minute
, len(replicate('0', 2 - len(convert(VARchar(2),datepart(hh,@Now)))) + convert(VARchar(2),datepart(hh,@Now))
+ replicate('0', 2 - len(convert(char(2),datepart(mi,@Now)))) + convert(char(2),datepart(mi,@Now)) )
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 28, 2008 at 9:10 am
November 28, 2008 at 9:21 am
Garadin (11/28/2008)
Heh, so the "local settings" were... a different time. :blush:
It's those pesky CHARs innit!
Sue, try this instead:
SELECT RIGHT('0' + CAST(datepart(hh,@Now) AS VARCHAR(2)),2)
+ RIGHT('0' + CAST(datepart(mi,@Now) AS VARCHAR(2)),2)
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply