Avatar of Taras
TarasFlag for Canada

asked on 

Date between does not work for some date range

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?

SQLMicrosoft SQL Server

Avatar of undefined
Last Comment
Taras
Avatar of Taras
Taras
Flag of Canada image

ASKER

I As Ord.[ORDER_START_Date_TIME]  is datetime field type I corrected my filter:

 Ord.[ORDER_START_Date_Time] Between @Start_Date And @End_Date and got the same error.
Avatar of ste5an
ste5an
Flag of Germany image

The question is: Why casting at all?

Store your ORDER_START_Date_TIME as DATETIME or DATE and your save.

Just be aware that you need to consider the time portion, thus you may need to use DATE.

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Taras
Taras
Flag of Canada image

ASKER

HI ste5an I tried your suggestion got the same error on DateAdd(dd,-20,GETDATE())  and more,  19 is working!?
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

For example, if then date is yesterday, and you used BETWEEN, then you would only get rows from yesterday where the time was exactly midnight, since the default time is midnight (00:00:00.000).  You'd miss yesterday at 9AM, for example.

By using >= and <. you get all times for yesterday.
Avatar of ste5an
ste5an
Flag of Germany image

The problem is your source. The data type of this column must be DATE or DATETIME.

E.g.

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

Open in new window

If this still raises your error, then you have stored a string which is not a date or date/time.

Use TRY_CAST():

SELECT TRY_CAST(Ord.[ORDER_START_Date_TIME] AS DATE) 
FROM yourTable;

Open in new window

To find the offending rows.
Avatar of Taras
Taras
Flag of Canada image

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.

Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

DECLARE @Start_Date DATETIME
DECLARE @End_Date   DATETIME
 
SET @Start_Date = DateAdd(dd,-15,CAST(GETDATE() AS date))
SET @End_Date = DateAdd(dd,-1,CAST(GETDATE() AS date))

Avatar of ste5an
ste5an
Flag of Germany image

No cast needed here:

User generated image
Avatar of Taras
Taras
Flag of Canada image

ASKER

Thank you both, in my select statement in list of fields I converted Order_Start_Date_Time to : CONVERT(VARCHAR,ord.[ORDER_START_Date_Time],111) and that trigger issue. I corrected that and after that it worked as you suggested.

Avatar of ste5an
ste5an
Flag of Germany image

This is a cast in the wrong place. Cause now you're doing a string comparision.
Avatar of Taras
Taras
Flag of Canada image

ASKER

I am surprised again it is not working...??

Avatar of ste5an
ste5an
Flag of Germany image

I'm not.

SELECT TRY_CAST(Ord.[ORDER_START_Date_TIME] AS DATE) 
FROM yourTable;

Open in new window

Avatar of Taras
Taras
Flag of Canada image

ASKER

when I run :
SELECT
TRY_CAST(Ord.[ORDER_START_Date_Time as DATE)
From
 MyTable ord
  Order By Ord.ORDER_START_DT_TM Desc
I got :
2021-11-23
2021-11-23
2021-11-23
2021-11-23
2021-11-23
2021-11-22
2021-11-19
2021-11-19
2021-11-19
2021-11-19
2021-11-17
2021-11-17
2021-11-17
2021-11-16
2021-11-16
2021-11-15
2021-11-15
2021-11-10
2021-11-10
......
Avatar of Taras
Taras
Flag of Canada image

ASKER

Order By Ord.ORDER_START_Date_Time Desc
Avatar of ste5an
ste5an
Flag of Germany image

You need to look for NULL values.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

I tried this:Ord.[ORDER_START_Date_TIME] >= @Start_Date And
Ord.[ORDER_START_Date_TIME] < DATEADD(DAY, 1, @End_Date)  the same error.

That's not possible if the Ord.[ORDER_START_Date_TIME] column is a date or datetime data type.

What is the data type of Ord.[ORDER_START_Date_TIME]?  If it's char/varchar (yuck!), what specific format is it stored in?

Avatar of Taras
Taras
Flag of Canada image

ASKER

it is Datetime:2021-11-23 14:52:13.707
Avatar of ste5an
ste5an
Flag of Germany image

Then it's as I initially wrote:

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

Open in new window

Without any CAST() or CONVERT().

Post your concrete SQL statement and the exact error message.
Avatar of Taras
Taras
Flag of Canada image

ASKER

Now it is working. Conversion error was triggered in another part of script Where I calculated Day of Stay with DATEDIFF function. I used it in CASE WHEN.....statement to count Length of Stay and at the end of  it  in "Else" part I had 'N/A' instead 0.
 So I apologize as  your suggestion was good and appropriate.
Microsoft SQL Server
Microsoft SQL Server

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.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo