December 11, 2002 at 12:41 pm
Add this to the very end of your script and run it.
print right('00' + @tmonth,2)
December 11, 2002 at 12:48 pm
Thank you. I added the print statement you recommended and it came back the same. What am I doing wrong?
December 11, 2002 at 12:58 pm
you can just add this select statement to your variable
select @Month = right(('0' + cast(datepart(mm, <yourdate>) as varchar(2), 2)
December 11, 2002 at 1:05 pm
Here try this:
select @Temp as '@Temp',@T1 as '@T1', substring(cast(100+month(@t1)as char(3)),2,3) as 'month1' , substring(cast(datepart(mm,@t1)+100 as char(3)),2,3) as 'month2'
Gregory Larsen, DBA
If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples
Gregory A. Larsen, MVP
December 11, 2002 at 4:15 pm
The problem comes from the fact that month() returns an int not a char, as does datepart. You will have to cast to a char to get it to work like you want.
Other option are
select replicate('0', 2 - len(month(@t1))) + cast(month(@t1) as varchar(2)) as x
or
DECLARE @m1 AS VARCHAR(2)
SET @m1 = cast(month(@t1) as varchar(2)) -- do this explicitly as SQl versions don't always handle the same and may not in the future.
select replicate('0', 2 - len(@m1)) + @m1 as x
Also, being that you are using SQL 2000 it might be more helpfull to write whatever you decide on into a UDF (User Defined Function) that way you can reuse with much less code.
December 11, 2002 at 8:52 pm
quote:
Here try this:select @Temp as '@Temp',@T1 as '@T1', substring(cast(100+month(@t1)as char(3)),2,3) as 'month1' , substring(cast(datepart(mm,@t1)+100 as char(3)),2,3) as 'month2'
Gregory Larsen, DBA
If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples
I used to do this all the time back in the old assembler programming days. It works well and is obvious once you start thinking RIGHT instead of left.
December 12, 2002 at 6:11 am
I would use
SELECT @TMonth = REPLACE(STR(MONTH(@T1),2,0),' ','0')
but then thats me.
Far away is close at hand in the images of elsewhere.
Anon.
December 12, 2002 at 8:54 am
Thank you all. With all your input, I solve the issue and successfully implementd it. Thank you again.
Viewing 8 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply