Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - Deleting rows between a date range using SQL. Date conversion fails

DELETE FROM BIZ 
WHERE [Orgnl_Cmpltn_Date]
      BETWEEN '2014-02-31'  AND '2014-04-01'

This is the DELETE statement I wrote. There is an error that says:

Conversion failed when converting date and/or time from character string.

I know I have to write the correct date format, but I am not sure how that goes.

This question has not been answered elsewhere because the answers I saw did not specify date format (in the context that I am asking for)

like image 809
ObserveDBA Avatar asked Oct 14 '15 07:10

ObserveDBA


People also ask

How do you DELETE data between two dates in SQL?

From which table do you want to delete the records? you just need to replace the select-command with an delete-command, followed by the table to delete the data from. Then remove the order-by-clause and that's it.

Is between inclusive for dates SQL?

The SQL BETWEEN Operator The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

How do I pass a date range in SQL query?

SELECT * FROM PERSONAL WHERE BIRTH_DATE_TIME BETWEEN '2000-01-01 00:00:00' AND '2002-09-18 12:00:00';

How do you fix the conversion of a varchar data type to a datetime data type resulted in an out of range value?

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. You need separators for the date like a “/”, a “.” or a “-“. We use substring to concatenate the “-” to use an acceptable date format and then we use the CONVERT function to convert the characters to sql date.


1 Answers

You wrote 31st of February... Maybe..... that date doesn't exists.

DELETE FROM BIZ 
WHERE [Orgnl_Cmpltn_Date]
BETWEEN '2014-02-28'  AND '2014-04-01'

For a general idea of convert date:

DELETE FROM BIZ 
WHERE [Orgnl_Cmpltn_Date]
BETWEEN CONVERT(date,'2014.02.28',102) and CONVERT(date,'2014.04.01',102)

Here you can find the complete list of values for third parameter of CONVERT https://msdn.microsoft.com/en-us/library/ms187928.aspx

like image 69
Galma88 Avatar answered Sep 30 '22 20:09

Galma88