🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #1035

Re: Round a DateTime

#1035
From: Quinn Wildman (CodeGear Develope...
Date: Mon, 19 Mar 2007 16:37
40 lines
2113 bytes
This answer already given is the best:

CAST(MyDateTimeField as Date)

However, if you subtract two timestamps you get a number. You can then 
do math on that. Example:

select

select current_timestamp-cast('1/1/0001' as timestamp) from rdb$database;

Robin wrote:
> Hi everyone,
> 
> I want to count distinct days in a period from a datetime field.  I am 
> surprised by how difficult I have found this.
> 
> My problem is in getting the date part of a datetime in SQL.
> 
> Is there a way to truncate a datetime to just a date?
> 
> I thought trunc or frac, but they are not available.  I can't seem to 
> cast the datetime as an integer either.
> 
> The only way I have found is to cast the datetime as varchar(11) which 
> happens to return only the date part.  Then cast THAT as an integer.
> 
> Which /appears/ to work, but makes me feel dirty ;-)
> 
> I am using Interbase6.x/FB1.5.  I am unable to make stored procedures, 
> UDF etc so I need to do it in raw SQL only.
> 
> Any ideas?
> 
> Thanks in advance.
> 

-- 
CREATE DOMAIN NEW_COMPANY AS CHAR(8) DEFAULT 'CODEGEAR' CHECK (VALUE <> 
'BORLAND') NOT NULL;
Technical Details
Message-ID: <45febc5a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45fe1225@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,cf698e5e2a79f5a0
X-Google-Attributes: gid114a0e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Mon, 19 Mar 2007 09:37:46 -0700
From: "Quinn Wildman (CodeGear Developer Support)" <qwildman@nospam.codegear.com>
User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version: 1.0
Newsgroups: borland.public.interbase.sql
Subject: Re: Round a DateTime
References: <45fe1225@newsgroups.borland.com>
In-Reply-To: <45fe1225@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 10.143.19.108
Message-ID: <45febc5a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1174322266 10.143.19.108 (19 Mar 2007 09:37:46 -0800)
Lines: 40
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail