| View previous topic :: View next topic |
| Author |
Message |
Cristian Luciano Custodio Guest
|
Posted: Wed Jul 07, 2004 2:50 pm Post subject: Truncate a DATE |
|
|
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
|
Posted: Wed Jul 07, 2004 7:28 pm Post subject: Re: Truncate a DATE |
|
|
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
|
Posted: Wed Jul 07, 2004 7:35 pm Post subject: Re: Truncate a DATE |
|
|
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
|
Posted: Wed Jul 07, 2004 7:58 pm Post subject: Re: Truncate a DATE |
|
|
"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
|
Posted: Wed Jul 07, 2004 9:16 pm Post subject: Re: Truncate a DATE |
|
|
"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
|
Posted: Wed Jul 07, 2004 10:17 pm Post subject: Re: Truncate a DATE |
|
|
| 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
|
Posted: Sun Jul 11, 2004 3:32 pm Post subject: Re: Truncate a DATE |
|
|
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( , @Date, 112))
tomi.
|
|
| Back to top |
|
 |
|