Sunday, February 19, 2012

Format a date/time field

Hello,
How can i format a date or time field in sql server?
I want to format a date like 10-10-2003 into october.
Thxuse the convert and give the style number
Check in BOL for convert
for eg
select convert(varchar,getdate(),110)
--
Shaju Thomas
e-Cosmos Technologies Ltd.,
Mail: shaju@.e-cosmostech.com
Phone(Off) : 51217037/38 Extn: 132
Mobile: +91 98455 21794
"Ezekiël" <ezekiel@.lycos.nl> wrote in message
news:ufHztJnrDHA.1764@.TK2MSFTNGP10.phx.gbl...
> Hello,
> How can i format a date or time field in sql server?
> I want to format a date like 10-10-2003 into october.
> Thx
>|||Shaju
Can you help me out with formating the date. I want e.g. 2003-01, where the first part is the year and the second the month. When i use the folowing statement
select month(getdate()) + space(1) + year(getdate()
The result is 2014
I can't find any examples with the operator function|||You should read about "datatype precedence" in Books Online. According to BOL, char is converted to
int, hence your result.
Use the CONVERT function instead:
SELECT CONVERT(CHAR(7), CURRENT_TIMESTAMP, 120)
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Ezekiël" <ezekil@.lycos.com> wrote in message
news:63705857-1841-4D58-A8CE-1CE91998EC45@.microsoft.com...
> Shaju,
> Can you help me out with formating the date. I want e.g. 2003-01, where the first part is the year
and the second the month. When i use the folowing statement:
> select month(getdate()) + space(1) + year(getdate())
> The result is 2014.
> I can't find any examples with the operator functions
>|||And how about if my datefield has text as the datatype, how does the code
look like?
"Tibor Karaszi" <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
wrote in message news:ORz5GDqrDHA.4092@.tk2msftngp13.phx.gbl...
> You should read about "datatype precedence" in Books Online. According to
BOL, char is converted to
> int, hence your result.
> Use the CONVERT function instead:
> SELECT CONVERT(CHAR(7), CURRENT_TIMESTAMP, 120)
> --
> Tibor Karaszi, SQL Server MVP
> Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
>
> "Ezekiël" <ezekil@.lycos.com> wrote in message
> news:63705857-1841-4D58-A8CE-1CE91998EC45@.microsoft.com...
> > Shaju,
> >
> > Can you help me out with formating the date. I want e.g. 2003-01, where
the first part is the year
> and the second the month. When i use the folowing statement:
> >
> > select month(getdate()) + space(1) + year(getdate())
> >
> > The result is 2014.
> >
> > I can't find any examples with the operator functions
> >
>|||exactly what do you have in that textcolumn? Can you show the data?
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Ezekiël" <ezekiel@.lycos.nl> wrote in message news:uTgkLtqrDHA.640@.tk2msftngp13.phx.gbl...
> And how about if my datefield has text as the datatype, how does the code
> look like?
> "Tibor Karaszi" <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
> wrote in message news:ORz5GDqrDHA.4092@.tk2msftngp13.phx.gbl...
> > You should read about "datatype precedence" in Books Online. According to
> BOL, char is converted to
> > int, hence your result.
> >
> > Use the CONVERT function instead:
> >
> > SELECT CONVERT(CHAR(7), CURRENT_TIMESTAMP, 120)
> >
> > --
> > Tibor Karaszi, SQL Server MVP
> > Archive at:
> http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
> >
> >
> > "Ezekiël" <ezekil@.lycos.com> wrote in message
> > news:63705857-1841-4D58-A8CE-1CE91998EC45@.microsoft.com...
> > > Shaju,
> > >
> > > Can you help me out with formating the date. I want e.g. 2003-01, where
> the first part is the year
> > and the second the month. When i use the folowing statement:
> > >
> > > select month(getdate()) + space(1) + year(getdate())
> > >
> > > The result is 2014.
> > >
> > > I can't find any examples with the operator functions
> > >
> >
> >
>

No comments:

Post a Comment