Unix Technical Forum

String/Date Concatenation causes conversion error - streamline fix suggestions

This is a discussion on String/Date Concatenation causes conversion error - streamline fix suggestions within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I'm trying to concatenate a Description (nchar(100)) and Date (datetime) as Description and my initial effort was just ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:40 PM
Chris H
 
Posts: n/a
Default String/Date Concatenation causes conversion error - streamline fix suggestions

Hi,
I'm trying to concatenate a Description (nchar(100)) and Date
(datetime) as Description and my initial effort was just
"...description+' '+open_date as description..." which throws a date/
string conversion error; finally came up with a working string below
but don't think it's the optimal way to do this - any suggestions?

select (rtrim(description)+'
'+rtrim(convert(char(2),datepart(mm,open_date)))
+'/'+convert(char(2),datepart(dd,open_date))
+'/'+convert(char(4),datepart(yyyy,open_date))) as description from
oncd_opportunity where opportunity_id=?

open_date is not a required field at the db level, but it is required
on the form so it should not be null as a rule.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:40 PM
Shiju Samuel
 
Posts: n/a
Default Re: String/Date Concatenation causes conversion error - streamline fix suggestions

You can use the third optional parameter of the convert to get the
date in the format you want.

convert(char(20),open_date,101)

-
Shiju



On Sep 4, 7:34 pm, Chris H <chollst...@broadreachpartnersinc.com>
wrote:
> Hi,
> I'm trying to concatenate a Description (nchar(100)) and Date
> (datetime) as Description and my initial effort was just
> "...description+' '+open_date as description..." which throws a date/
> string conversion error; finally came up with a working string below
> but don't think it's the optimal way to do this - any suggestions?
>
> select (rtrim(description)+'
> '+rtrim(convert(char(2),datepart(mm,open_date)))
> +'/'+convert(char(2),datepart(dd,open_date))
> +'/'+convert(char(4),datepart(yyyy,open_date))) as description from
> oncd_opportunity where opportunity_id=?
>
> open_date is not a required field at the db level, but it is required
> on the form so it should not be null as a rule.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:40 PM
Chris H
 
Posts: n/a
Default Re: String/Date Concatenation causes conversion error - streamline fix suggestions

Shiju,

Thanks - the working (and streamlined) query becomes:

select (rtrim(description)+' '+convert(char(20),open_date,101)) as
description from oncd_opportunity where opportunity_id=?

Much more elegant - thanks!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:22 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com