I have a MVC project with two fields where the user can input two dates.
Model:
[DisplayName("Date From")]
public string DateFrom { get; set; }
[DisplayName("Date To")]
public string DateTo { get; set; }
The two dates are belonging to one column in the database called OriginalDate
. This daterange is the result.
In the database the column OriginalDate
is a character varying
field. The syntax is DD.MM.YYYY
.
When the user's input is "Date From" 14.02.2017
(DD.MM.YYYY
) and the "Date To" 20.02.2019
, how can I reach the result, that my query finds all the results between those two dates?
Considered this statement:
SELECT * FROM tbl.tbl
WHERE OriginalDate BETWEEN '14.02.2017' AND '20.02.2019'
This is giving me a result, but also from other years. For example DD.MM.1998
or DD.MM.2009
. How I have to cast that this statement is working?
Then I tried TO_DATE
:
SELECT * FROM tbl.tbl
WHERE OriginalDate BETWEEN TO_DATE('14.02.2017', 'DD.MM.YYYY')
AND TO_DATE('20.02.2019', 'DD.MM.YYYY')
There I get this error:
LINE 1: SELECT * FROM tbl.tbl WHERE OriginalDate BETWEEN TO_DAT...
^
HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
SQL state: 42883
Furthermore is my question, if I cast it to a date, will it find it when the column is a character variable?
How can I solve this problem?