Solved

MS sql - Conversion failed when converting date and/or time from character string

Posted on 2012-03-19
6
555 Views
Last Modified: 2012-04-01
Hi
I just created SP as follows, but failed as captioned.

USE [Report]
GO
/****** Object:  StoredProcedure [dbo].[test]    Script Date: 03/19/2012 15:41:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[test]

@DB_name nvarchar(50) ,
/****@FromDate datetime,****/
@ToDate datetime,
@Posted nvarchar(3)

AS
BEGIN
SET NOCOUNT ON;
SET @DB_name = RTRIM(@DB_name)

DECLARE @cmd AS NVARCHAR(max)


SET @cmd = N'SELECT * FROM ' + @DB_name + '.dbo.R_Voucher where Posted in (' + @Posted +') and voucherdate between ' + convert(datetime,@ToDate,4) +'and' + @ToDate
EXEC sp_executesql @cmd

END
0
Comment
Question by:beckyng
  • 3
6 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 350 total points
ID: 37736691
Your convert doesn't make sense. You need to
a) convert to varchar
b) enclose the resulting date string in single quotes
c) do that for both parts of the between
and pay attention to missing blanks between values and keywords.

Try this example to check for the difference:
declare @dt datetime
set @dt = '20120201 3:4'
select @dt, convert(datetime, @dt, 4), convert(varchar, @dt, 4)

Open in new window

Further I discourage usage of country-specific date formats, like 4 for German; you should use ISO (112) or ODBC (120) instead, as that works always, and does not allow for misinterpretations by the SQL  engine
Correct code:
USE [Report]
GO
/****** Object:  StoredProcedure [dbo].[test]    Script Date: 03/19/2012 15:41:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[test]

@DB_name nvarchar(50) ,
/****@FromDate datetime,****/
@ToDate datetime,
@Posted nvarchar(3)

AS
BEGIN
SET NOCOUNT ON;
SET @DB_name = RTRIM(@DB_name)

DECLARE @cmd AS NVARCHAR(max)


SET @cmd = N'SELECT * FROM ' + @DB_name + '.dbo.R_Voucher where Posted in (' + @Posted +') and voucherdate between ''' + convert(varchar,@ToDate,112) +''' and ''' + convert(varchar, @ToDate, 120) + ''''
EXEC sp_executesql @cmd 

END

Open in new window

0
 
LVL 6

Assisted Solution

by:Peter Kiprop
Peter Kiprop earned 150 total points
ID: 37736713
Hi beckyng,

The date conversion style of 4 is wrong. Please use any of what is shown in the below link

http://msdn.microsoft.com/en-us/library/ms187928.aspx

Regards,
Peter
0
 

Author Comment

by:beckyng
ID: 37736725
I just amend the statement as follows, but failed "Arithmetic overflow error converting expression to data type datetime."


USE [Report]
GO
/****** Object:  StoredProcedure [dbo].[test]    Script Date: 03/19/2012 15:41:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[test]

@DB_name nvarchar(50) ,
@FromDate datetime,
@ToDate datetime,
@Posted nvarchar(3)

AS
BEGIN
SET NOCOUNT ON;
SET @DB_name = RTRIM(@DB_name)

DECLARE @cmd AS NVARCHAR(max)


SET @cmd = N'SELECT * FROM ' + @DB_name + '.dbo.R_Voucher where Posted in (' + @Posted +')and voucherdate between ' + convert(varchar,@FromDate,112) + ' and ' + convert(varchar,@ToDate,112)
EXEC sp_executesql @cmd

END
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 37737184
You are missing the single quotes inside the quotes - please review my code.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 37741370
Does http:#a37736713, providing a simple link for the relevant part of the Books Online really qualify as answer here? I have shown exact details of what to use, and it hasn't been just a simple "use the right convert" case.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

773 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