How to Subtract day on date-time column - SQL Server 2008

sql subtract days from current date
sql date - 30 days
sql getdate minus 1 day in where clause
sql subtract date from current date
sql server datediff
date add sql
sql current date minus 1 year
sql datediff between two dates

I have two columns which have YYYY MM DD MM SS format and I want to subtract days between these two columns (DD)

201810031030 YYYY MM DD MM SS - 201810051030 YYYY MM DD MM SS  = 3(03-05)

I have a SQL function which converts MM Month to string(i.e January).

Any suggestion highly appreciated

Below is the SQL and the error I get:


Conversion failed when converting date and/or time from character string

Currently the datatype is VARCHAR (201810031030) and I would like to cast it on the fly

You can use the DateDiff function:


How to Subtract day on date-time column - SQL Server 2008, We can use DATEADD() function like below to Subtract days from DateTime in Sql Server. DATEADD() functions first parameter value can be day or dd or d all  Add 30 days to a date SELECT DATEADD(DD,30,@Date) Add 3 hours to a date SELECT DATEADD(HOUR,-3,@Date) Subtract 90 minutes from date SELECT DATEADD(MINUTE,-90,@Date) Check out the chart to get a list of all options.

The optimal solution would be to change your database structure using the correct data type (datetime).

If this is not possible you can use stuff to build a standard datetime format and do a cast to datetime. Then you can use datediff to calculate difference in days:

declare @tmp table (date_1 varchar(50), date_2 varchar(50))

insert into @tmp values ('201810031030','201810051030')

     cast(stuff(stuff(date_1, 11,0,':') + ':00', 9,0,' ') as datetime) as date_1
    ,cast(stuff(stuff(date_2, 11,0,':') + ':00', 9,0,' ') as datetime) as date_2

        cast(stuff(stuff(date_1, 11,0,':') + ':00', 9,0,' ') as datetime),
        cast(stuff(stuff(date_2, 11,0,':') + ':00', 9,0,' ') as datetime)
    ) + 1 as diff
from @tmp


How to Subtract Days from DateTime in Sql Server, The DATEADD() function adds a time/date interval to a date and then returns the date. month, mm, m = month; dayofyear, dy, y = Day of the year; day, dd, d = Day; week, Works in: SQL Server (starting with 2008), Azure SQL Database, Azure SQL Add 18 years to the date in the BirthDate column, then return the date:. How to Subtract Months from DateTime in Sql Server? We can use DATEADD() function like below to Subtract Months from DateTime in Sql Server. DATEADD() functions first parameter value can be month or mm or m, all will return the same result. Below example shows how we can Subtract two months from Current DateTime in Sql Server:

You would need to basically do something like this to keep it pretty standard and simple. This is basically bad practice from a database point of view but it is possible that there is a legitimate need for the data in that format so, I am not one to judge. This should get you what you are looking for with each execution:

DECLARE @oddDTFormat1 AS CHAR(12);
DECLARE @oddDTFormat2 AS CHAR(12);

SET @oddDTFormat1 = '201809010030';
SET @oddDTFormat2 = '201809031037';

SET @newDTFormat1 = CONVERT(DATETIME, LEFT(@oddDTFormat1, 8), 112) + CONVERT(DATETIME, CONCAT(SUBSTRING(@oddDTFormat1, 9, 2), ':', SUBSTRING(@oddDTFormat1, 11, 2)), 114);

SET @newDTFormat2 = CONVERT(DATETIME, LEFT(@oddDTFormat2, 8), 112) + CONVERT(DATETIME, CONCAT(SUBSTRING(@oddDTFormat2, 9, 2), ':', SUBSTRING(@oddDTFormat2, 11, 2)), 114);

SELECT DATEDIFF(DAY, @newDTFormat1, @newDTFormat2);

Thanks, Dave Betournay PV9685

SQL Server DATEADD() Function, 2005, SQLServer 2008, SQLServer 2008 R2 > How to add/subtract days from a date – TSQL To add or subtract days from a date, you can simply use: SELECT DATEADD(DAY, 10, GETDATE()), '10 Days Later' two dates – TSQL How to extract day/month/year from a DateTime column – TSQL. Second parameter is start date. This could be smalldatetime or datetime data type or string that is formatted as a date. Third parameter is ending date of time span. Note, if smalldatetime is used, seconds and milliseconds will always be zero. DATEDIFF function returns integer value. For example, if you try to find out how much hours are in 61 minutes DATEDIFF will return that there are 2 hours.

Sql And MeHow to add/subtract days from a date – TSQL, See Date and Time Data Types and Functions (Transact-SQL) for an the number of day boundaries crossed between dates in two columns in a table. SQL This works because in T-SQL it is possible to add (or subtract) two DATETIME values, and you’ll find that it produces exactly the desired result, which is: 2015-08-27 15:33:21.057. Knowing from

DATEDIFF (Transact-SQL), You can also subtract a number of units from a date/time value by Also see Date, Time, and DateTime data types and working with dates and  B. Using date subtraction. The following example subtracts a number of days from a datetime date. Applies to: SQL Server and SQL Database.-- Uses AdventureWorks DECLARE @altstartdate datetime; SET @altstartdate = CONVERT(DATETIME, ''January 10, 1900 3:00 AM', 101); SELECT @altstartdate - 1.5 AS 'Subtract Date'; Here is the result set:

DateAdd, DateDiff, and TimeZoneOffset functions , The DATEADD() function adds a number to a specified date part of an input of type DATE , DATETIME , DATETIMEOFFSET , DATETIME2 , SMALLATETIME , or day, dd, d. week, wk, ww. hour, hh. minute, mi, n. second, ss, s. millisecond, ms Drop Schema · Create New Table · Identity Column · Sequence · Add Column  Can a DATETIME Value Be Subtracted from Another DATETIME Value? - A collection of 18 FAQs on Transact-SQL language date and time operations and functions. Clear answers are provided with tutorial exercises on converting date and time values to integers

  • If you have date time data, you really should be storing it using the proper datatype -- DATE, TIME, DATETIME, etc. Do so, and all your date time manipulations problems become much, much simpler to resolve.
  • Are they really space separated?
  • @Salman A totally agree I must have overlooked at the datatype.
  • Please forgive me the code formatting this machine is not set up for SQL so I am working in a browser and validating with web tools. ;)
  • Also, if you care about performance, reliability, and accuracy of this code and plan to perform operations on your Date/Time data for years to come you would want to do something like create a new table and use the code I supplied to create a table of DATETIME data. To support legacy code use something like a view to turn the DATETIME into the proprietary format you are currently using. If this is part of a decently sized business it would be wise to do this before the database becomes even more complex, or after years when employees change and confusion sets in.