Solved

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

Posted on 2012-03-19
6
549 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 68

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 68

Expert Comment

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

919 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

14 Experts available now in Live!

Get 1:1 Help Now