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,'/'+datename(dd,'/'+datename(yy, as Date',
datename(hh,':'+datename(mi,':'+datename(ss, as 'Time',
Is there a cleaner and more efficient way of doing this?
Who is Participating?
grant300Connect With a Mentor Commented:
I believe so.

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

.... convert(,1) as DateCol,
     convert(,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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.