snippetsqlModerate
How to include a datetime parameter within a stored procedure along with string query?
Viewed 0 times
storedwithincludequeryprocedurewithinhowalongstringparameter
Problem
My stored procedure is as follows,
Executing this string query results in following error,
"Conversion failed when converting date and/or time from character string."
Any one please help me to sort this out...
-- Add the parameters for the stored procedure here
@FromDate datetime,
@ToDate datetime
--Select query
DECLARE @query nvarchar(max)
set @query='SELECT [col1]
FROM [Table1]
WHERE ([col2] BETWEEN '''+@FromDate+''' AND'''+@ToDate+''')'
execute sp_executesql @queryExecuting this string query results in following error,
"Conversion failed when converting date and/or time from character string."
Any one please help me to sort this out...
Solution
The fundamental issue is that TSQL cannot implicitly convert datetime (or integer or floating point) into character data types. It actually goes backwards, it attempts to implicitly convert the character data to the datetime (int/floating point value) based on the data type precedence rules. This is what your error message is telling you by the way, it couldn't convert the strings to datetime values. You have to explicitly ask it to mash values into character strings.
This example demonstrates the principal with integers as they are easier to understand expected values.
The value of the implicit conversion shows the @stringint is converted to integer first and then the + is treated as numeric addition and results in 8. The explicit conversion of @intint to a character data type results in the + sign treated as concatenation with the returned string of 35
To solve your supplied problem, you must explicitly cast your datetime values to a character type so the query string can be concatenated as expected.
But as indicated above, you really don't want to do that for a number of reasons, SQL Injection being one of them. It also makes your maintenance much harder when you're slicing and dicing a query string in TSQL.
A better approach is to parameterize your query and use the power of sp_executesql. A nice thing about the sp_executesql parameter is that you don't have to use all the parameters supplied. Depending on what you are truly trying to do, this may be beneficial.
Sample table and data
Demo of using parameters
Results
This example demonstrates the principal with integers as they are easier to understand expected values.
DECLARE
@stringInt nvarchar(3)
, @intint int
SELECT
@stringInt = N'3'
, @intint = 5
SELECT
@stringInt + @intint AS implicit_conversion
, @stringInt + CAST(@intint AS nvarchar(5)) AS explicit_conversionThe value of the implicit conversion shows the @stringint is converted to integer first and then the + is treated as numeric addition and results in 8. The explicit conversion of @intint to a character data type results in the + sign treated as concatenation with the returned string of 35
implicit_conversion explicit_conversion
------------------- -------------------
8 35To solve your supplied problem, you must explicitly cast your datetime values to a character type so the query string can be concatenated as expected.
set @query='SELECT [col1]
FROM [Table1]
WHERE ([col2] BETWEEN ''' + CONVERT(nvarchar(24), @FromDate, 121) +''' AND'''+ CONVERT(nvarchar(24), @ToDate, 121) +''')'But as indicated above, you really don't want to do that for a number of reasons, SQL Injection being one of them. It also makes your maintenance much harder when you're slicing and dicing a query string in TSQL.
A better approach is to parameterize your query and use the power of sp_executesql. A nice thing about the sp_executesql parameter is that you don't have to use all the parameters supplied. Depending on what you are truly trying to do, this may be beneficial.
Sample table and data
CREATE TABLE
dbo.table1
(col1 int, col2 datetime)
INSERT INTO
dbo.table1
SELECT
3, '2009-04-06'
UNION ALL SELECT
1, '2001-09-11'Demo of using parameters
DECLARE
@FromDate datetime,
@ToDate datetime
SELECT
@FromDate = '2005-03-17'
, @ToDate = current_timestamp
DECLARE
@query nvarchar(max)
SET @query = N'
SELECT [col1]
FROM [Table1]
WHERE ([col2] BETWEEN @start AND @end)'
-- gratuitous use of parameter assignment here
-- could just as easily used @FromDate and @ToDate
-- in the @query and the parameter list
EXECUTE sp_executesql
@query
, N'@start datetime, @end datetime'
, @start = @FromDate
, @end = @ToDateResults
col1
3Code Snippets
DECLARE
@stringInt nvarchar(3)
, @intint int
SELECT
@stringInt = N'3'
, @intint = 5
SELECT
@stringInt + @intint AS implicit_conversion
, @stringInt + CAST(@intint AS nvarchar(5)) AS explicit_conversionimplicit_conversion explicit_conversion
------------------- -------------------
8 35set @query='SELECT [col1]
FROM [Table1]
WHERE ([col2] BETWEEN ''' + CONVERT(nvarchar(24), @FromDate, 121) +''' AND'''+ CONVERT(nvarchar(24), @ToDate, 121) +''')'CREATE TABLE
dbo.table1
(col1 int, col2 datetime)
INSERT INTO
dbo.table1
SELECT
3, '2009-04-06'
UNION ALL SELECT
1, '2001-09-11'DECLARE
@FromDate datetime,
@ToDate datetime
SELECT
@FromDate = '2005-03-17'
, @ToDate = current_timestamp
DECLARE
@query nvarchar(max)
SET @query = N'
SELECT [col1]
FROM [Table1]
WHERE ([col2] BETWEEN @start AND @end)'
-- gratuitous use of parameter assignment here
-- could just as easily used @FromDate and @ToDate
-- in the @query and the parameter list
EXECUTE sp_executesql
@query
, N'@start datetime, @end datetime'
, @start = @FromDate
, @end = @ToDateContext
StackExchange Database Administrators Q#7967, answer score: 11
Revisions (0)
No revisions yet.