473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Casting timestamp with time zone to varchar automatically

Is there any reason why there isn't a predefined cast to go from a
timestamp to a varchar? Is there a reason not to add one?
--
Jim C. Nasby, Database Consultant ji*@nasby.net
Member: Triangle Fraternity, Sports Car Club of America
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
6 3398
Unfortunatly, that still doesn't really answer my question. I have a
generic function that accepts varchars, and I'd like to be able to feed
it timestamps without explicitly converting. That's why I'd like to know
if there is a specific reason there's no default timestamp -> varchar
cast.

On Tue, Aug 03, 2004 at 03:57:36PM +0900, Michael Glaesemann wrote:

On Aug 3, 2004, at 3:12 PM, Jim C. Nasby wrote:
Is there any reason why there isn't a predefined cast to go from a
timestamp to a varchar? Is there a reason not to add one?


to_char should do what you need. People often need a specific form of
timestamp if they need it to be a text value (rather than a native
timestamp data type). to_char provides lots of flexibility
Michael Glaesemann
grzm myrealbox com


--
Jim C. Nasby, Database Consultant ji*@nasby.net
Member: Triangle Fraternity, Sports Car Club of America
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
"Jim C. Nasby" <de*****@decibel.org> writes:
... I'd like to know
if there is a specific reason there's no default timestamp -> varchar
cast.


There is an explicit cast from timestamp to varchar, at least in recent
releases:

regression=# select 'now'::timestamp::varchar;
varchar
---------------------------
2004-08-04 00:42:05.34875
(1 row)

Whether this should be invokable implicitly is somewhat of a theological
issue, but personally I'm agin it. My experience is that implicit
cross-type-category casts are Bad News All Around because they tend to
happen when you weren't expecting it, resulting in quite surprising
behavior. (An implicit cast from, say, timestamp to date is far less
dangerous.) You can find lots of discussion about related issues in
the list archives.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #3

Idea :

Create a function with the same name as your function, but which takes a
timestamp as an argument, converts it to a string according to your
specifications, then calls your function which needs a string.

Postgresql will decide which function to call according to the types of
the arguments.

Unfortunatly, that still doesn't really answer my question. I have a
generic function that accepts varchars, and I'd like to be able to feed
it timestamps without explicitly converting. That's why I'd like to know
if there is a specific reason there's no default timestamp -> varchar
cast.

On Tue, Aug 03, 2004 at 03:57:36PM +0900, Michael Glaesemann wrote:

On Aug 3, 2004, at 3:12 PM, Jim C. Nasby wrote:
>Is there any reason why there isn't a predefined cast to go from a
>timestamp to a varchar? Is there a reason not to add one?


to_char should do what you need. People often need a specific form of
timestamp if they need it to be a text value (rather than a native
timestamp data type). to_char provides lots of flexibility
Michael Glaesemann
grzm myrealbox com


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4
"Jim C. Nasby" <de*****@decibel.org> writes:
On Wed, Aug 04, 2004 at 12:48:57AM -0400, Tom Lane wrote:
Whether this should be invokable implicitly is somewhat of a theological
issue, but personally I'm agin it. My experience is that implicit
cross-type-category casts are Bad News All Around because they tend to
happen when you weren't expecting it, resulting in quite surprising
behavior. (An implicit cast from, say, timestamp to date is far less
dangerous.) You can find lots of discussion about related issues in
the list archives.
Actually, my experience has been that the real issue isn't cross-type,
it's loss of information. For example, automatically casting a timestamp
to a date means you lose information; if this happens automatically you
can be in for a very unpleasant surprise (I was recently bit by this
when doing division of a double or a numeric and having it get converted
to an int because I was dividing by an int).


Yeah, that is certainly bad, but cross-category is bad news for different
reasons. The sort of example I've seen come up again and again is that
someone compares a foo to a bar and files a bug report because the
comparison is behaving in a wacko fashion. On investigation it turns
out that there is no foo-to-bar comparison operator, but the system
decided it could implicitly cast both of them to text and do a textual
comparison. The behavior is perfectly sensible when seen as a text
comparison but made no sense in terms of the original datatypes'
semantics. Type casts within a category tend not to have such problems
because, for example, timestamps and dates sort compatibly in any case.

So I do not like implicit casts to text from non-textual datatypes, and
would like to get rid of the ones we have rather than introduce more.

I think we have already cleaned up all the cases where
information-losing casts were marked implicit, but we still have some
implicit casts to text :-(

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5
On Wed, Aug 04, 2004 at 12:48:57AM -0400, Tom Lane wrote:
"Jim C. Nasby" <de*****@decibel.org> writes:
... I'd like to know
if there is a specific reason there's no default timestamp -> varchar
cast.
There is an explicit cast from timestamp to varchar, at least in recent
releases:

regression=# select 'now'::timestamp::varchar;
varchar
---------------------------
2004-08-04 00:42:05.34875
(1 row)

Whether this should be invokable implicitly is somewhat of a theological
issue, but personally I'm agin it. My experience is that implicit
cross-type-category casts are Bad News All Around because they tend to
happen when you weren't expecting it, resulting in quite surprising
behavior. (An implicit cast from, say, timestamp to date is far less
dangerous.) You can find lots of discussion about related issues in
the list archives.


Actually, my experience has been that the real issue isn't cross-type,
it's loss of information. For example, automatically casting a timestamp
to a date means you lose information; if this happens automatically you
can be in for a very unpleasant surprise (I was recently bit by this
when doing division of a double or a numeric and having it get converted
to an int because I was dividing by an int). I would argue that any
conversion where you won't lose information (ie: timestamp to text) is
OK.

In any case, turned out that wasn't my problem anyway...
regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


--
Jim C. Nasby, Database Consultant de*****@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #6
On Wed, Aug 04, 2004 at 01:57:12PM -0400, Tom Lane wrote:
"Jim C. Nasby" <de*****@decibel.org> writes:
On Wed, Aug 04, 2004 at 12:48:57AM -0400, Tom Lane wrote:
Whether this should be invokable implicitly is somewhat of a theological
issue, but personally I'm agin it. My experience is that implicit
cross-type-category casts are Bad News All Around because they tend to
happen when you weren't expecting it, resulting in quite surprising
behavior. (An implicit cast from, say, timestamp to date is far less
dangerous.) You can find lots of discussion about related issues in
the list archives.

Actually, my experience has been that the real issue isn't cross-type,
it's loss of information. For example, automatically casting a timestamp
to a date means you lose information; if this happens automatically you
can be in for a very unpleasant surprise (I was recently bit by this
when doing division of a double or a numeric and having it get converted
to an int because I was dividing by an int).


Yeah, that is certainly bad, but cross-category is bad news for different
reasons. The sort of example I've seen come up again and again is that
someone compares a foo to a bar and files a bug report because the
comparison is behaving in a wacko fashion. On investigation it turns
out that there is no foo-to-bar comparison operator, but the system
decided it could implicitly cast both of them to text and do a textual
comparison. The behavior is perfectly sensible when seen as a text
comparison but made no sense in terms of the original datatypes'
semantics. Type casts within a category tend not to have such problems
because, for example, timestamps and dates sort compatibly in any case.

So I do not like implicit casts to text from non-textual datatypes, and
would like to get rid of the ones we have rather than introduce more.

I think we have already cleaned up all the cases where
information-losing casts were marked implicit, but we still have some
implicit casts to text :-(


Would it maybe make more sense to change things so that implicit casts
aren't used for comparisons? Maybe instead of the simple 3-tier system
of what casts can do there should be a bitmap that specifies if a cast
can happen implitily for function calls, column assignments, and/or
comparisons.
--
Jim C. Nasby, Database Consultant de*****@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kingofkolt | last post by:
All, What do you recommend as the best type of field for a timestamp in a MySQL database? I've considered varchar(10) or INT but I'm not sure what is the best, most efficient, and most reliable...
2
by: Chris Ochs | last post by:
I am trying to move all of the sql statements from my code into a pl/pgsql function. I have one function for a transaction block that contains 10 inserts and I need to pass 50 parameters to it. ...
1
by: Will Trillich | last post by:
not sure what it's supposed to look like, but i'm reasonably sure 'show time zone' shouldn't produce this-- db=# set timezone = '-6'; SET db=# show timezone; TimeZone ----------------...
1
by: mboes | last post by:
Hi there, Is there a way of specifying the precision of a timestamp *with timezone* field? It works fine for timestamps *without* timezones, but gives me a syntax error if I try with...
2
by: Zygo Blaxell | last post by:
I have a table with a few million rows of temperature data keyed by timestamp. I want to group these rows by timestamp intervals (e.g. every 32 seconds), compute aggregate functions on the...
1
by: Antony Paul | last post by:
Hi all What is the last value in a column of data type timestamp without time zone. A sample data is 2004-06-22 18:07:00.486027. What does 486027 mean Milliseconds or seconds since midnight or...
5
by: Prabu Subroto | last post by:
Dear my friends... I created some tables with field timestamp (datatype also timestamp). I mean, I want to have the data when each record inserted or modified in the tables. on MysQL, I just...
4
by: P. George | last post by:
i have a table with a 'timstamp with time zone' column. when i insert into it: '18 Nov 2004 00:00:00 PST' ....it looks like: 2004-11-18 03:00:00-05
7
by: JJ | last post by:
How do I set one field to have the updated timestamp, and another to have the created timestamp? I want to do this directly from code generated from DB Designer if possible?! JJ
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.