开发者

MSSQL changing a return value only if it pass a Boolean_expression

开发者 https://www.devze.com 2023-03-31 07:10 出处:网络
i want to do something like this SELECT (开发者_如何学运维if T.EndTime = \'00:00:00\' THEN \'23:59:59\' ELSE T.EndTime) AS EndTime from Table AS T

i want to do something like this

SELECT (开发者_如何学运维if T.EndTime = '00:00:00' THEN '23:59:59' ELSE T.EndTime) AS EndTime from Table AS T

i know the solution should be simple but i just cant find away to make it work


You want a CASE:

http://msdn.microsoft.com/en-us/library/ms181765.aspx

SELECT
   CASE T.EndTime
      WHEN '00:00:00' THEN '23:59:59'
      ELSE T.EndTime
   END
FROM ...


CASE is what you are looking for:

SELECT (CASE WHEN T.EndTime = '00:00:00' 
             THEN '23:59:59' 
             ELSE T.EndTime END) AS EndTime 
from Table AS T


SELECT 
      CASE EndTime
         WHEN '00:00:00' THEN '23:59:59'
         ELSE EndTime
      END


If the column T.EndTime is of data type DateTime, you might want to convert the value to string first. So, the query would be something like this. Value 8 used in Convert function returns datetime in the format hh:mi:ss. You can find the list of formats for Cast or Convert at MSDN website.

SELECT  CASE 
            WHEN CONVERT(VARCHAR, T.EndTime, 8) = '00:00:00' THEN '23:59:59'
            ELSE T.EndTime
        END
FROM    dbo.TableData T
0

精彩评论

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