Search code examples
sqlsql-serverdateadd

How do I convert a value after using DATEADD with it


I have a little query that strips the date from the datetime field but when I try to convert it from GMT to CST it readds the date. Is there a better way to do this?

Location table:

arrival
4-6-2018 12:35:43




SELECT arrival
FROM(
SELECT CONVERT(VARCHAR(8), arrival))
FROM locations
)a

This query will give me this result:

12:35:43

SELECT (DATEADD(hour,-5,arrival))
FROM(
SELECT CONVERT(VARCHAR(8), arrival))
FROM locations
)a

4-6-2018 12:35:43

This query will give readd the date. How can I remove the date and then do the dateadd function without it readding the date


Solution

  • Probably this is what you are asking for:

    SELECT Convert(Varchar(8), DATEADD(hour,-5,arrival), 108)
    FROM locations;
    

    Note: This is compatible with SQL server versions that doesn't have Time datatype too.