• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

SYBASE ASE DATE/TIME PARSING

Using Sybase ASE 12.5.4 and I need to parse a datetime entry into a date and time column separately, separately.  I am currently using the datename function to build a string for each column, as seen below:
(date is a datetime field)
datename(mm,cmt.date)+'/'+datename(dd,cmt.date)+'/'+datename(yy,cmt.date) as Date',
datename(hh,cmt.date)+':'+datename(mi,cmt.date)+':'+datename(ss,cmt.date) as 'Time',
Is there a cleaner and more efficient way of doing this?
0
mynus482
Asked:
mynus482
1 Solution
 
grant300Commented:
I believe so.

You can use the convert function with the correct STYLE specifier and get what you want directly.

.... convert(cmt.date,1) as DateCol,
     convert(cmt.date,8) as TimeCol

The style number specifiers are documented in the T-SQL Reference Manual: Building Blocks.

Style 1 converts to MM/DD/YY and Style 8 converts to HH:MM:SS, both of which are what I believe you were specifying above.

BTW, storing dates and times as strings greatly reduces their utility in the database.  Sorting order is all wrong, date math is out of the question unless you are willing to write very complex and inefficient expressions that convert the strings back to dates first.  Since you can always get the strings out of dates or, better yet, datatime datatypes, the best practice rule of thumb is to store them as such rather than as strings.

Regards,
Bill
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now