?
Solved

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

Posted on 2012-03-19
6
Medium Priority
?
587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 1400 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 600 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 70

Expert Comment

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

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

777 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