Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

828 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