Re: convert a date to a string?
From: Kalen Delaney (replies_at_public_newsgroups.com)
Date: 09/16/04
- Next message: alien2_51: "Re: Creating a DTS ActiveX Task"
- Previous message: Jay: "Shape SQL"
- In reply to: Fred: "Re: convert a date to a string?"
- Next in thread: Toby Herring: "Re: convert a date to a string?"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 16 Sep 2004 14:17:30 -0700
Fred
The 3rd argument to convert is primarily used to format a datetime when
converting it to a CHARACTER string.
Look again to find the numeric code for the style you want, then convert to
a varchar...
SELECT CONVERT(varchar(30), '1/1/2004', style-number)
Also, don't convert it back to a datetime, or put it in a datetime variable,
or you'll lose the format. Datetime values cannot be formatted. Only when
they exist as a string...
-- HTH ---------------- Kalen Delaney SQL Server MVP www.SolidQualityLearning.com "Fred" <anonymous@discussions.microsoft.com> wrote in message news:1e4301c49b8f$07492800$a601280a@phx.gbl... >I have already tried > > select Convert(datetime, '1/1/2004', 'dd-mm-yy') > > but got the error message that > "Argument data type varchar is invalid for argument 3 of > convert function", if I remove the single quotes I > get "Invalid column name" > > How do I enter dd-mm-yy > > >>-----Original Message----- >>Yes, read the docs in Books Online for CONVERT and check > out the 3rd >>parameter. >> >>-- >>HTH >>---------------- >>Kalen Delaney >>SQL Server MVP >>www.SolidQualityLearning.com >> >> >>"Fred" <anonymous@discussions.microsoft.com> wrote in > message >>news:305b01c49b79$56d0f760$a501280a@phx.gbl... >>> Yes, I want to select a date column from a table and > view >>> it as a string. Example '1/1/2004' would > be 'January- >>> 04', '2/15/2004' would be 'February-2004'. I was > going to >>> write a UDF where I manually pick the Month and Year > of my >>> date and say >>> >>> Declare @m int >>> Declare @y int >>> Declare @mon varchar(20) >>> Declare @yr varchar(2) >>> Declare @result varchar(22) >>> >>> Set @m = Month(@myDate) >>> Set @y = Year(@myDate) >>> If @m = 1 >>> Set @mon = 'January' >>> ... >>> Set @result = @mon + '-' + @yr >>> Return @result >>> >>> Is there a better way to do this? Like in MS Access > they >>> have the Format Function. Is it possible to use > Convert >>> to do something like this? >>> >>> TIA >>> Fred >> >> >>. >>
- Next message: alien2_51: "Re: Creating a DTS ActiveX Task"
- Previous message: Jay: "Shape SQL"
- In reply to: Fred: "Re: convert a date to a string?"
- Next in thread: Toby Herring: "Re: convert a date to a string?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|