ravi@sql (5/9/2013)
Hi All,select Case when MONTH(GETDATE())=1
then 12
when LEN(MONTH(GETDATE()))=2 then MONTH(GETDATE())
else right('0'+ convert(varchar(2), MONTH(GETDATE())),4) end
In the above query else statment will come it as to come with zero(0) as prefix. AM expecting 05 as result . but am getting only 5 . zero is not considering as varchar.
Please let me know the solution for this ?::(
Regards,
Ravi@sql
Data type precedence:
SELECT CASE
WHEN MONTH(GETDATE()) = 1 THEN '12'
WHEN LEN(MONTH(GETDATE())) = 2 THEN CAST(MONTH(GETDATE()) AS VARCHAR(2))
ELSE RIGHT('0'+ convert(varchar(2), MONTH(GETDATE())),2)
END
INT has a higher data type precedence than VARCHAR, so each choice in the CASE was implicitly cast to INT. Avoiding mixing data types in CASE expressions is good practice.
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