I am testing my SQL Server script with date range and I have set up two variable Start and End date.
DECLARE @Start_Date DATETIME
DECLARE @End_Date DATETIME
SET @Start_Date = DateAdd(dd,-15,GETDATE())
SET @End_Date = DateAdd(dd,-1,GETDATE())
In Where part of script have I am filtering records :
Ord.[ORDER_START_Date_TIME] Between Convert(Varchar,@Start_Date,111) And Convert(Varchar,@End_Date,111)
If I set up SET @Start_Date = DateAdd(dd,-15,GETDATE())
It is working and bringing result, if I put days all way up to 19 it is working and bringing records back, when setup start date like:
SET @Start_Date = DateAdd(dd,-20,GETDATE())
I got error:
Msg 245, Level 16, State 1, Line 16
Conversion failed when converting the ****** value '******' to data type ******.
Any Idea what could be wrong?
DECLARE @Start_Date DATE = DateAdd(dd,-15,GETDATE());
DECLARE @End_Date DATE = DateAdd(dd,-1,GETDATE());
Ord.[ORDER_START_Date_TIME] BETWEEN @Start_Date AND @End_Date
ASKER
DECLARE @Start_Date DATE = DateAdd(dd,-15,GETDATE());
DECLARE @End_Date DATE = DateAdd(dd,-1,GETDATE());
CAST(Ord.[ORDER_START_Date_TIME] AS DATE) BETWEEN @Start_Date AND @End_Date
If this still raises your error, then you have stored a string which is not a date or date/time.SELECT TRY_CAST(Ord.[ORDER_START_Date_TIME] AS DATE)
FROM yourTable;
To find the offending rows.
ASKER
ASKER
ASKER
SELECT TRY_CAST(Ord.[ORDER_START_Date_TIME] AS DATE)
FROM yourTable;
ASKER
ASKER
I tried this:Ord.[ORDER_START_Date_TIME] >= @Start_Date And
Ord.[ORDER_START_Date_TIME] < DATEADD(DAY, 1, @End_Date) the same error.
ASKER
DECLARE @Start_Date DATE = DateAdd(dd,-15,GETDATE());
DECLARE @End_Date DATE = DateAdd(dd,-1,GETDATE());
Ord.[ORDER_START_Date_TIME] BETWEEN @Start_Date AND @End_Date
Without any CAST() or CONVERT().ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
ASKER
Ord.[ORDER_START_Date_Time] Between @Start_Date And @End_Date and got the same error.