Sunday 1 January 2012

How to Cast a String Date to TimeStamp?

When we are dealing with string you need to cast the date string to date and afterwards the date to a timestamp.


Example :


select cast(cast('2011-07-11' as date) as timestamp(0));

Note that above assumes  'YYYY-MM-DD' date format.

If the date string has a different format then try the following

select cast(('2011/07/11' (date, format'YYYY/DD/MM')) as timestamp(0))
where you can explicit specify your date format...
Additional Examples:

     Insert into dt
     Select cast(cast('2011/07/11' as date ) as timestamp(6) );

     Insert into dt
     Select cast(cast('2011/07/11' as date format 'yyyy/mm/dd') as timestamp(6));

No comments:

Post a Comment