Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

cast date time to string

 
   Database Help (Home) -> Programming RSS
Next:  Dynamic SQL  
Author Message
iccsi

External


Since: May 05, 2011
Posts: 7



(Msg. 1) Posted: Thu May 05, 2011 12:25 pm
Post subject: cast date time to string
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have following code to cast datepart to a char, but I get
'Incorrect syntax near 'cast', expected 'AS'.'

Select cast(datepart(yyyy,weekending),char(4)) + '-' +
cast(datepart(m, weekending),char(2)) + '-' + cast(datepart(d,
weekending),char(2)) as weekending from #w

Are there any way to cast date part to a char variable?

Your help is great appreciated,

 >> Stay informed about: cast date time to string 
Back to top
Login to vote
iccsi

External


Since: May 05, 2011
Posts: 7



(Msg. 2) Posted: Thu May 05, 2011 1:55 pm
Post subject: Re: cast date time to string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 5, 4:05 pm, "Bob Barrows" wrote:
> iccsi wrote:
> > I have following code to cast datepart to a char, but I get
> > 'Incorrect syntax near 'cast', expected 'AS'.'
>
> The error message is pretty explicit ....
>
> This
> > cast(datepart(yyyy,weekending),char(4)) + '-' +
>
> should be
> cast(datepart(yyyy,weekending) AS char(4))
>
> >    cast(datepart(m, weekending),char(2)) + '-' + cast(datepart(d,
> > weekending),char(2)) as weekending from #w
>
> You're going to too much trouble. The CONVERT function has a style argument
> that lets you specify this particular format:
>
> select CONVERT(varchar(10),weekending,120) as weekending from #w
>
> The 120 specifies yyyy-mm-dd hh:mi:ss(24h)
> The varchar(10) uses just the first 10 characters: yyyy-mm-dd

Thanks for helping,
My date separator is ".", is it possible to change to "-"

Thanks again,

 >> Stay informed about: cast date time to string 
Back to top
Login to vote
iccsi

External


Since: May 05, 2011
Posts: 7



(Msg. 3) Posted: Thu May 05, 2011 2:39 pm
Post subject: Re: cast date time to string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks again,
I used type 102 instead of 120
One I use 120 then i have date separator correct,

Thanks again,


On May 5, 4:55 pm, iccsi wrote:
> On May 5, 4:05 pm, "Bob Barrows" wrote:
>
>
>
>
>
> > iccsi wrote:
> > > I have following code to cast datepart to a char, but I get
> > > 'Incorrect syntax near 'cast', expected 'AS'.'
>
> > The error message is pretty explicit ....
>
> > This
> > > cast(datepart(yyyy,weekending),char(4)) + '-' +
>
> > should be
> > cast(datepart(yyyy,weekending) AS char(4))
>
> > >    cast(datepart(m, weekending),char(2)) + '-' + cast(datepart(d,
> > > weekending),char(2)) as weekending from #w
>
> > You're going to too much trouble. The CONVERT function has a style argument
> > that lets you specify this particular format:
>
> > select CONVERT(varchar(10),weekending,120) as weekending from #w
>
> > The 120 specifies yyyy-mm-dd hh:mi:ss(24h)
> > The varchar(10) uses just the first 10 characters: yyyy-mm-dd
>
> Thanks for helping,
> My date separator is ".", is it possible to change to "-"
>
> Thanks again,- Hide quoted text -
>
> - Show quoted text -
 >> Stay informed about: cast date time to string 
Back to top
Login to vote
Bob Barrows

External


Since: Jan 05, 2011
Posts: 4



(Msg. 4) Posted: Thu May 05, 2011 4:05 pm
Post subject: Re: cast date time to string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

iccsi wrote:
> I have following code to cast datepart to a char, but I get
> 'Incorrect syntax near 'cast', expected 'AS'.'

The error message is pretty explicit ....
>
This
> cast(datepart(yyyy,weekending),char(4)) + '-' +
should be
cast(datepart(yyyy,weekending) AS char(4))


> cast(datepart(m, weekending),char(2)) + '-' + cast(datepart(d,
> weekending),char(2)) as weekending from #w
>

You're going to too much trouble. The CONVERT function has a style argument
that lets you specify this particular format:

select CONVERT(varchar(10),weekending,120) as weekending from #w


The 120 specifies yyyy-mm-dd hh:mi:ss(24h)
The varchar(10) uses just the first 10 characters: yyyy-mm-dd
 >> Stay informed about: cast date time to string 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 5) Posted: Thu May 05, 2011 6:25 pm
Post subject: Re: cast date time to string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

iccsi ( ) writes:
> On May 5, 4:05 pm, "Bob Barrows" wrote:
>> iccsi wrote:
>> The 120 specifies yyyy-mm-dd hh:mi:ss(24h)
>> The varchar(10) uses just the first 10 characters: yyyy-mm-dd
>
> Thanks for helping,
> My date separator is ".", is it possible to change to "-"

Had you consulted the topic CAST and CONVERT in Books Online, you would
have seen that Bob slipped on the number. The format code you want is 121.


--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
 >> Stay informed about: cast date time to string 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Date Only from Date/Time data - I am trying to query a column in a SQL database with date/time data in it which looks exactly like this example: 05/16/2006 08:33:14 AM. I have two queries. In one query I want to return only the date half of the info. In the other query I want to..

how to convert a date string to datetime - Hi, I just found 'Aug-14-1973' isdate returns 0, how to convert a string like this into sql datetime formmat, mybe 'Aug/14/1973' too. Thanks.

Between date/time - Is there a way to determine in sql 2000 if a date & time is between a date & time range? For instance, one event is 01/02/07 between 2:00 and 3:00 and the 2nd event is 01/02/07 and starts at 1:30. I want to check that the 2nd event is not be...

DateTime, Date and Time? - Is there a reason sqlserver doesn't have a Date and a Time datatype? I can kind of see why there is no Date data type but why not time? Are they trying to future proof sqlserver by not locking into the speed at which this particular planet rotates?

Is this a Valid Date Time - Hello, Is this a valid date time format for use in SQL2005? 2006-09-25-16.49.45 The data is coming from an Interbase DB and the application was designed in Germany. Right now SQL does not recognize it as a valid date time field. Thanks
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]