开发者

The best way to add a concatenated string of multiple fields to a stored procedure

开发者 https://www.devze.com 2023-02-15 09:25 出处:网络
I have 14 datetime fields in my database. Ive been asked to return a single field of the 14 dates as one string field. Right now my stored proc looks kinda like this:

I have 14 datetime fields in my database. Ive been asked to return a single field of the 14 dates as one string field. Right now my stored proc looks kinda like this:

convert(varchar, [DTMON_F],108) as mondayFrom,
   convert(varchar,[DTMON_T],108) as mondayTo,
   convert(varchar,[DTTUES_F],108) as tuesdayFrom,
开发者_JAVA技巧   convert(varchar,[DTTUES_T],108) as tuesdayTo,

I want to have a single field called extendedDetails that is in the format HHmm - HHmm, HHmm - HHmm

This returns for example "10:20:00" so I'll have to somehow cut this to look like "1020" and then somehow concat all of them.

convert(varchar, [DTMON_F],108) as mondayFrom


SELECT 
  REPLACE
  ( 
   convert(char(5), [DTMON_F],108) + ' - ' +  
   convert(char(5),[DTMON_T],108) + ' - ' +
   convert(char(5),[DTTUES_F],108) + ' - ' +
   convert(char(5),[DTTUES_T],108) + ' - ' +
    ...
     , ':', '') AS whatever 
FROM MYTABLE 
0

精彩评论

暂无评论...
验证码 换一张
取 消