?
Solved

Convert varchar to a date

Posted on 2009-12-17
6
Medium Priority
?
212 Views
Last Modified: 2012-05-08
I have a tale that I populate with a select query.

The field in question is called [05_Appt Date] [varchar](50) NULL, 22/12/09
I want to convert the field to a date.

when i test the query i get.
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.

USE [Reports]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE sp_rpt

	@FromDate DATETIME,
	@ToDate DATETIME

AS

BEGIN

	SELECT

		[03_PCode]	
	,	[04_Name]
	,	CONVERT(DATETIME,[05_Appt Date],103) AS [05_Appt Date]
	,	[07_TYPE]
	,	[09_Address]
	,	[12_email]

	FROM [rptEngineersSchedule]
	WHERE [05_Appt Date]
	BETWEEN @FromDate AND @ToDate	

END

Open in new window

0
Comment
Question by:aneilg
6 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26071898
0
 
LVL 26

Expert Comment

by:tigin44
ID: 26071920
try this
USE [Reports]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE sp_rpt

        @FromDate DATETIME,
        @ToDate DATETIME

AS

BEGIN

        SELECT

                [03_PCode]      
        ,       [04_Name]
        ,       CONVERT(DATETIME,STUFF([05_Appt Date], 7, 0, '20' ),103) AS [05_Appt Date]
        ,       [07_TYPE]
        ,       [09_Address]
        ,       [12_email]

        FROM [rptEngineersSchedule]
        WHERE [05_Appt Date]
        BETWEEN @FromDate AND @ToDate   

END

Open in new window

0
 

Author Comment

by:aneilg
ID: 26071948
If i try

,      dbo.ufn_GetDateOnly(rpt.[05_Appt Date])

I get, Msg 242, Level 16, State 3, Line 2
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 26071966
ALTER PROCEDURE sp_rpt

      @FromDate varchar(10),
      @ToDate varchar(10)

AS

BEGIN

      SELECT

            [03_PCode]      
      ,      [04_Name]
      ,      CONVERT(DATETIME,[05_Appt Date],103) AS [05_Appt Date]
      ,      [07_TYPE]
      ,      [09_Address]
      ,      [12_email]

      FROM [rptEngineersSchedule]
      WHERE [05_Appt Date]
      BETWEEN convert(datetime,  @FromDate,3 )  AND convert(datetime, @ToDate, 3 )      

END
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 2000 total points
ID: 26071978
alternatively you can pass the date in this format  YYYYmmdd
0
 

Author Comment

by:aneilg
ID: 26072177
cool, thats what i've done.

Thanks for your help.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

612 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