Solved

Convert DateTime to VarChar

Posted on 2006-06-28
14
17,532 Views
Last Modified: 2008-07-14
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
Comment
Question by:ChristianKullenbo
14 Comments
 

Author Comment

by:ChristianKullenbo
Comment Utility
I should add that in is only when the GETDATE Function i used I get the error
0
 
LVL 25

Expert Comment

by:dstanley9
Comment Utility
try

SET @strShowing = '(Showing.ShowingDate BETWEEN ''' + CONVERT (VARCHAR(24), GETDATE(),121) + ''' AND ''' + CONVERT(VARCHAR,@ShowingTo) + ''') AND'
0
 
LVL 3

Expert Comment

by:RickBeebe
Comment Utility
The date conversion works fine for me.....

Is @strShowing long enough to accept the entire string?

DECLARE @strShowing VARCHAR(2000)....
0
 
LVL 3

Expert Comment

by:RickBeebe
Comment Utility
The default length for VARCHAR is 30.
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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
 

Author Comment

by:ChristianKullenbo
Comment Utility
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
 
LVL 3

Expert Comment

by:RickBeebe
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:ChristianKullenbo
Comment Utility
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
 

Author Comment

by:ChristianKullenbo
Comment Utility
strange because the varchar vas set to 100 that should be enough..
0
 

Author Comment

by:ChristianKullenbo
Comment Utility
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
 
LVL 3

Accepted Solution

by:
RickBeebe earned 250 total points
Comment Utility
SELECT CONVERT(VARCHAR,GETDATE(),110)
0
 

Author Comment

by:ChristianKullenbo
Comment Utility
I would like the format yyyy-mm-dd without the time. Is there a predefined date format for that?
0
 
LVL 28

Expert Comment

by:rafrancisco
Comment Utility
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
 

Author Comment

by:ChristianKullenbo
Comment Utility
Thanx!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Truncate vs Delete 63 87
INSERT INTO SELECT JOIN THING 2 24
Extract XML Data from using TSQL 5 28
while loop in html mail format 5 32
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now