• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 17551
  • Last Modified:

Convert DateTime to VarChar

Why won´t this work?

"Microsoft OLE DB Provider for SQL Server (0x80040E57)
String or binary data would be truncated."
To me it appears that it wont convert the Date to VarChar Datatype. If I insert a date directly or via a variable like:

SET @tempVar = '2004-12-12' instead of the CONVERT it works.

SET @strShowing = '(Showing.ShowingDate BETWEEN ''' + CONVERT (VARCHAR, GETDATE(),121) + ''' AND ''' + CONVERT(VARCHAR,@ShowingTo) + ''') AND'
0
ChristianKullenbo
Asked:
ChristianKullenbo
1 Solution
 
ChristianKullenboAuthor Commented:
I should add that in is only when the GETDATE Function i used I get the error
0
 
dstanley9Commented:
try

SET @strShowing = '(Showing.ShowingDate BETWEEN ''' + CONVERT (VARCHAR(24), GETDATE(),121) + ''' AND ''' + CONVERT(VARCHAR,@ShowingTo) + ''') AND'
0
 
RickBeebeCommented:
The date conversion works fine for me.....

Is @strShowing long enough to accept the entire string?

DECLARE @strShowing VARCHAR(2000)....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RickBeebeCommented:
The default length for VARCHAR is 30.
0
 
Brendt HessSenior DBACommented:
This error is almost certainly only peripherally related to the GETDATE conversion.  What this error indicates is that an insert or update has pushed the total amount of data on a record beyond the 8,000 character hard limit that Microsoft SQL allows in a single row (excluding blob-types such as text or image).

The easiest way to be certain would be to cause your code to exit immediately after the SET @strShowing statement.  If this does not generate an error, then the error isn't in this line, but in something further down the road.
0
 
ChristianKullenboAuthor Commented:
Strange, the @strShowing is what you see, max varchar(100) (havent counted)
this variable is part of a larger string a nvarchar.. but that is set to 2000 and as a said erlier when i hardcode the date there is no problem.. And if i exlclude the @strShowing there is no problems...
0
 
RickBeebeCommented:
SELECT CONVERT(VARCHAR,GETDATE(),121)

Returns:

'2006-06-28 15:13:22.123'

Did you include the time when you hard coded the date?
0
 
ChristianKullenboAuthor Commented:
No I didnt.
The SELECT Convert works for me to..in the query analyzer....

But, when i use your time as a variable I get the same error, so i has something to do with the length...
0
 
ChristianKullenboAuthor Commented:
strange because the varchar vas set to 100 that should be enough..
0
 
ChristianKullenboAuthor Commented:
Maybe it is because i converted to string and my string is not between quotes in the query so it is interpreted as two diffrent values (the datepart and the timepart )
How do i cut off the timepart?
0
 
RickBeebeCommented:
SELECT CONVERT(VARCHAR,GETDATE(),110)
0
 
ChristianKullenboAuthor Commented:
I would like the format yyyy-mm-dd without the time. Is there a predefined date format for that?
0
 
rafranciscoCommented:
To format the date without time in the yyyy-mm-dd, you can do the following:

SELECT CONVERT(VARCHAR(10), GETDATE(), 120) AS [YYYY-MM-DD]

For additional date formats, you can refer to the following link:

http://www.sql-server-helper.com/tips/date-formats.aspx
0
 
ChristianKullenboAuthor Commented:
Thanx!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now