Friday, February 10, 2012

Convert Date to String

Hello, I have a date field in a table. I don't need the time part. I want
the result to be in the format YYYYMMDD. Consulted BOL and it gave me:
SELECT CONVERT (char, fieldname, 8). Tried that and I get the time hours.
How can I get it to read from the left and only give me YYYYMMDD? Thanks,
PanchoSELECT CONVERT(CHAR(8), ColumnName, 112)
"Pancho" <Pancho@.discussions.microsoft.com> wrote in message
news:D45536FD-271B-413F-BAFC-5EB418FC7FCE@.microsoft.com...
> Hello, I have a date field in a table. I don't need the time part. I
> want
> the result to be in the format YYYYMMDD. Consulted BOL and it gave me:
> SELECT CONVERT (char, fieldname, 8). Tried that and I get the time hours.
> How can I get it to read from the left and only give me YYYYMMDD? Thanks,
> Pancho|||Aaron,
This is great, worked fine. It required me to add the phrase AS ColumnName
and then it worked. I had tried styles 1 through 16 and stopped. I got a
message, and then assumed that there were no higher style numbers. This
taught me the danger of assuming. Thanks for your quick response. Have a
nice wend, Pancho.
"Aaron Bertrand [SQL Server MVP]" wrote:

> SELECT CONVERT(CHAR(8), ColumnName, 112)
>
> "Pancho" <Pancho@.discussions.microsoft.com> wrote in message
> news:D45536FD-271B-413F-BAFC-5EB418FC7FCE@.microsoft.com...
>
>|||> This is great, worked fine. It required me to add the phrase AS
> ColumnName
> and then it worked. I had tried styles 1 through 16 and stopped. I got a
> message, and then assumed that there were no higher style numbers. This
> taught me the danger of assuming.
Yes, you should learn searching instead. :-)
http://www.aspfaq.com/2464|||Bookmarked aspfaq.com! Thanks again, P.
"Aaron Bertrand [SQL Server MVP]" wrote:

> Yes, you should learn searching instead. :-)
> http://www.aspfaq.com/2464
>
>

No comments:

Post a Comment