I want to select a varchar field as a date field
For example a field has this value "30.12.2011 21:15:03"
and when i select this
select DATE from TABLE where DATE = '30.12.2011'
i get no result.
I want to select a varchar field as a date field
For example a field has this value "30.12.2011 21:15:03"
and when i select this
select DATE from TABLE where DATE = '30.12.2011'
i get no result.
select DATE from TABLE where cast(DATE as date) = '30.12.2011'
Date field is a timestamp
You ask about getting the date part of a timestamp field, but what your question is actually about is filtering on the date of a timestamp field. There is a much simpler method of accomplishing that: you can use the knowledge that all the possible timestamps on a specific date won't have any timestamps for different dates between them.
Your edit explains that you haven't got a timestamp field at all. Nevertheless, a similar approach may still work:
Or the Firebird-specific
Assuming the field is a date field, use the
DATE
introducer combined withyyyy-mm-dd
(orTIMESTAMP
with time as well).So use:
Technically you can leave off the introducer, but it is 'correcter' in the light of the SQL standard.
Assuming a
TIMESTAMP
field, you won't get results unless the timestamp is (always) at00:00:00.0000
(in which case it should have been aDATE
instead).For the comparison to work, you need to use either
BETWEEN
, eg:or truncate the timestamp to a date (this may adversely effect performance if the timestamp is indexed, because then the index can no longer be used), eg:
If the date is stored in a
VARCHAR
field (which in itself is a bad idea), there are several solutions, first is to handle it as date manipulation:The double cast is required if you have a time-component in
VARCHARFIELD
as well. This assumes dates in the supported format listed below. If you useBETWEEN
as above, you can use a single cast to timestamp)The other solution (as suggested by hvd) is to treat it purely as string manipulation, for example:
This has its own set of problems if you want to select ranges. Bottomline: use a real
TIMESTAMP
field!Note that Firebird supports multiple formats:
yyyy-mm-dd
, eg 2014-05-25 (ISO-8601 format, probably best to use as it reduces confusion)dd.mm.yyyy
, eg 25.05.2014mm/dd/yyyy
, eg 05/25/2014mm-dd-yyyy
, eg 05-25-2014dd mmm yyyy
, eg 25 MAY 2014 (+ variations with a-
,.
or/
as separator)mmm dd yyyy
, eg MAY 25 2014 (+ variations with a-
,.
or/
as separator)Here is the answere to my question:
This took me 5 hours to find this out, maybe there should be a "-" instead of "." but it works.