Friday, February 24, 2012

DATEPART with 2 digits

I am trying to get the day or month part of the date that are less than 10
with 2 digits but I can not get it.
For example I want to get 07 and not only 7. Is there a way I can get this.
Following is what I am using but it does not give me what I want:
SELECT LEFT(DATEPART(m, GETDATE()),2)
Thanks for any helpTry,
SELECT right('0' + ltrim(DATEPART(m, GETDATE())), 2)
AMB
"DXC" wrote:
> I am trying to get the day or month part of the date that are less than 10
> with 2 digits but I can not get it.
> For example I want to get 07 and not only 7. Is there a way I can get this.
> Following is what I am using but it does not give me what I want:
> SELECT LEFT(DATEPART(m, GETDATE()),2)
> Thanks for any help
>|||As DATEPART returns an integer value, you cannot start using string
functions upon it. If you want a string, try this:
SELECT LEFT('0'+CONVERT(varchar(2), DATEPART(m, GETDATE())), 2)
R
"DXC" <DXC@.discussions.microsoft.com> wrote in message
news:CAF52094-754B-4E57-87BD-501247EBA683@.microsoft.com...
> I am trying to get the day or month part of the date that are less than 10
> with 2 digits but I can not get it.
> For example I want to get 07 and not only 7. Is there a way I can get
this.
> Following is what I am using but it does not give me what I want:
> SELECT LEFT(DATEPART(m, GETDATE()),2)
> Thanks for any help
>|||Thanks......That did it but I hope I get the correct dates for the dates 10
and grater.
"Alejandro Mesa" wrote:
> Try,
> SELECT right('0' + ltrim(DATEPART(m, GETDATE())), 2)
>
> AMB
>
> "DXC" wrote:
> > I am trying to get the day or month part of the date that are less than 10
> > with 2 digits but I can not get it.
> >
> > For example I want to get 07 and not only 7. Is there a way I can get this.
> > Following is what I am using but it does not give me what I want:
> >
> > SELECT LEFT(DATEPART(m, GETDATE()),2)
> >
> > Thanks for any help
> >|||Ooops, silly me. Of course it should have been RIGHT, not LEFT, as Alejandro
has posted.
"R" <anon@.spamme.please> wrote in message
news:e9RAZOKVFHA.544@.TK2MSFTNGP15.phx.gbl...
> As DATEPART returns an integer value, you cannot start using string
> functions upon it. If you want a string, try this:
> SELECT LEFT('0'+CONVERT(varchar(2), DATEPART(m, GETDATE())), 2)
> R
> "DXC" <DXC@.discussions.microsoft.com> wrote in message
> news:CAF52094-754B-4E57-87BD-501247EBA683@.microsoft.com...
> > I am trying to get the day or month part of the date that are less than
10
> > with 2 digits but I can not get it.
> >
> > For example I want to get 07 and not only 7. Is there a way I can get
> this.
> > Following is what I am using but it does not give me what I want:
> >
> > SELECT LEFT(DATEPART(m, GETDATE()),2)
> >
> > Thanks for any help
> >
>|||That is fine.....I tested and it works with the dates beyond 10. I just did
not want to end up with the dates like 012 or 027.
Thanks.
"R" wrote:
> Ooops, silly me. Of course it should have been RIGHT, not LEFT, as Alejandro
> has posted.
> "R" <anon@.spamme.please> wrote in message
> news:e9RAZOKVFHA.544@.TK2MSFTNGP15.phx.gbl...
> > As DATEPART returns an integer value, you cannot start using string
> > functions upon it. If you want a string, try this:
> >
> > SELECT LEFT('0'+CONVERT(varchar(2), DATEPART(m, GETDATE())), 2)
> >
> > R
> >
> > "DXC" <DXC@.discussions.microsoft.com> wrote in message
> > news:CAF52094-754B-4E57-87BD-501247EBA683@.microsoft.com...
> > > I am trying to get the day or month part of the date that are less than
> 10
> > > with 2 digits but I can not get it.
> > >
> > > For example I want to get 07 and not only 7. Is there a way I can get
> > this.
> > > Following is what I am using but it does not give me what I want:
> > >
> > > SELECT LEFT(DATEPART(m, GETDATE()),2)
> > >
> > > Thanks for any help
> > >
> >
> >
>
>

No comments:

Post a Comment