BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Truncate a DATE

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Cristian Luciano Custodio
Guest





PostPosted: Wed Jul 07, 2004 2:50 pm    Post subject: Truncate a DATE Reply with quote



How can I do to truncate a date?

I have a date: 01/01/2004 15:20:01 and I need to convert to: 01/01/2004
00:00:00

In PostgreSQL and Interbase I can use the ANSI function CAST('01/01/2004
15:20:01' as DATE),
but SQL Server it didn't work

Cristian


Back to top
David Gagnon
Guest





PostPosted: Wed Jul 07, 2004 7:28 pm    Post subject: Re: Truncate a DATE Reply with quote



This is an ugly way to get what you need.

select
cast(cast(datepart(mm,columnname) as varchar(2)) + '/' +
cast(datepart(dd,columnname) as varchar(2)) + '/' +
cast(datepart(yyyy,columnname) as varchar(4)) as datetime)
from tablename



"Cristian Luciano Custodio" <cristian_custodio (AT) terra (DOT) com.br> wrote in
message news:40ec0de4$1 (AT) newsgroups (DOT) borland.com...
Quote:
How can I do to truncate a date?

I have a date: 01/01/2004 15:20:01 and I need to convert to: 01/01/2004
00:00:00

In PostgreSQL and Interbase I can use the ANSI function CAST('01/01/2004
15:20:01' as DATE),
but SQL Server it didn't work

Cristian





Back to top
David Gagnon
Guest





PostPosted: Wed Jul 07, 2004 7:35 pm    Post subject: Re: Truncate a DATE Reply with quote



Oops - forgot an easier way

select
cast (left(columnname, 12) as datetime)
from tablename

"David Gagnon" <dgagnonATchartermiDOTnet> wrote

Quote:
This is an ugly way to get what you need.

select
cast(cast(datepart(mm,columnname) as varchar(2)) + '/' +
cast(datepart(dd,columnname) as varchar(2)) + '/' +
cast(datepart(yyyy,columnname) as varchar(4)) as datetime)
from tablename



"Cristian Luciano Custodio" <cristian_custodio (AT) terra (DOT) com.br> wrote in
message news:40ec0de4$1 (AT) newsgroups (DOT) borland.com...
How can I do to truncate a date?

I have a date: 01/01/2004 15:20:01 and I need to convert to: 01/01/2004
00:00:00

In PostgreSQL and Interbase I can use the ANSI function CAST('01/01/2004
15:20:01' as DATE),
but SQL Server it didn't work

Cristian







Back to top
Mike Walsh
Guest





PostPosted: Wed Jul 07, 2004 7:58 pm    Post subject: Re: Truncate a DATE Reply with quote


"Cristian Luciano Custodio" <cristian_custodio (AT) terra (DOT) com.br> wrote in
message news:40ec0de4$1 (AT) newsgroups (DOT) borland.com...
Quote:
How can I do to truncate a date?

I have a date: 01/01/2004 15:20:01 and I need to convert to: 01/01/2004
00:00:00

In PostgreSQL and Interbase I can use the ANSI function CAST('01/01/2004
15:20:01' as DATE),
but SQL Server it didn't work

Cristian


Cristian,

That's because MS SQL doesn't have a DATE or TIME, only DATETIME. You can do
as David suggested, though that relies on the formatting of the date as a
string, and I don't know if that's regionally dependent. Another approach is
to take the floor of the date like...

cast (floor (cast (YourDate as float)) as datetime)

Mike Walsh



Back to top
David Freeman
Guest





PostPosted: Wed Jul 07, 2004 9:16 pm    Post subject: Re: Truncate a DATE Reply with quote

"Cristian Luciano Custodio" <cristian_custodio (AT) terra (DOT) com.br> wrote in
message news:40ec0de4$1 (AT) newsgroups (DOT) borland.com...
Quote:
How can I do to truncate a date?

I have a date: 01/01/2004 15:20:01 and I need to convert to: 01/01/2004
00:00:00

In PostgreSQL and Interbase I can use the ANSI function CAST('01/01/2004
15:20:01' as DATE),
but SQL Server it didn't work

Cristian


Hi,

The above suggestions will work on the database side.
In delphi you can do datevariable := trunc(datevariable). A date
(tdatetime) is just a double.

David



Back to top
Craig van Nieuwkerk
Guest





PostPosted: Wed Jul 07, 2004 10:17 pm    Post subject: Re: Truncate a DATE Reply with quote

Quote:

Hi,
The above suggestions will work on the database side.
In delphi you can do datevariable := trunc(datevariable). A date
(tdatetime) is just a double.


Or "datevariable := DateOf(datevariable);" which does the same thing but is
more explanatory IMO.



Back to top
Tomislav Kardaš
Guest





PostPosted: Sun Jul 11, 2004 3:32 pm    Post subject: Re: Truncate a DATE Reply with quote

Hi!

On Wed, 7 Jul 2004 11:50:48 -0300, "Cristian Luciano Custodio"
<cristian_custodio (AT) terra (DOT) com.br> wrote:

Quote:
How can I do to truncate a date?

SELECT @TheDate = convert(datetime, convert(varchar(Cool, @Date, 112))

tomi.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.