Date Convertion
-
Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI
-
Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI
You can do something like below.
DECLARE @Input as varchar(10) = '2011-11-06'
select case when datepart(day,@Input) <= 9 then
cast('0' + cast(datepart(day,@Input) as varchar(1)) as varchar(2))
else cast(datepart(day,@Input) as varchar(2)) end AS NewDateRegards
-
Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI
If you have always same format of datetime yyyy-MM-dd then you can use substring e.g
select substring('2011-11-06',9,2)
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com
-
If you have always same format of datetime yyyy-MM-dd then you can use substring e.g
select substring('2011-11-06',9,2)
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com
If they are using a date column, I think this would be better
select substring(Cast(@mydate as varchar(10)),9,2)
-
Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI
How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)
modified on Tuesday, June 7, 2011 3:49 PM
-
How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)
modified on Tuesday, June 7, 2011 3:49 PM
left? :doh:
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3modified on Friday, June 10, 2011 8:41 PM
-
left? :doh:
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3modified on Friday, June 10, 2011 8:41 PM
Good catch... fixed. It DID produce the correct answer, but that was happenstance, not design.
-
Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI
-
How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)
modified on Tuesday, June 7, 2011 3:49 PM
gr8...exactly what i required.. thankyou
-
How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)
modified on Tuesday, June 7, 2011 3:49 PM
Good call :) Have my 5.
Regards
-
Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI
SELECT CONVERT(VARCHAR(10), GETDATE(), 105)</pre>
Place your date inplace of Gatedate()