convert string to date and add days - sql

I have a string in this format: yyyyMMdd
and i want to add a Year for every date in this format.
How can i do that?

For SQL Server it should be
select
DATEADD(day, your_number_of_day, convert(datetime, your_date_column, 102))
from
your_table

Related

MMYYYY compare to YYYYMMDD

i have dates stored as chars in db (i know, but its not my db nor my idea..).
One of the dates is stored as MMYYYY (012016,022016...) and the second one is stored as YYYYMMDD (20160101,20160202...).
Is there a way to compare those dates? I need to take one date and select all of the second dates which are at least one year older then the first one...
Thank you for any help !
So for example i have Date1 field with values : 012014,012015,012016
and Date2 field with value: 20141005
And i need only Date1 which is at least one year older then Date2 so in this case it will return only 012016
I make two ctes to show how to work it out. I assume MMYYYY are stored as nvarchar or varchar and YYYYMMDD are the same type. If not - you will need one/few conversions on YYYYMMDD field.
;WITH cte AS (
SELECT '012016' as MMYYYY
UNION ALL
SELECT '022016'
), cte2 AS (
SELECT '20160101' AS YYYYMMDD
UNION ALL
SELECT '20160202'
)
SELECT *
FROM cte c
INNER JOIN cte2 c2
ON YYYYMMDD LIKE RIGHT(MMYYYY,4)+LEFT(MMYYYY,2) +'%'
Output:
MMYYYY YYYYMMDD
012016 20160101
022016 20160202
EDIT:
To find out differences in years use this:
DATEDIFF(year,CAST(YYYYMMDD as date), CAST(RIGHT(MMYYYY,4)+LEFT(MMYYYY,2)+'01' as date))
You can convert both fields into type DATE, then compare them.
For MMYYYY, you can use the function DATEFROMPARTS() to build a date. For example, convert 012014 into date:
-- DATEFROMPARTS(year, month, day)
DATEFROMPARTS(RIGHT('012014', 4), LEFT('012014', 2), 1)
For YYYYMMDD, it is easier, because this is the format ISO with Time Style 112. You can use the CONVERT() function to do the conversion :
CONVERT(DATE, '20141005', 112)
And here's how the final query looks like :
WITH casted AS (
SELECT
DATEFROMPARTS(RIGHT(date1, 4), LEFT(date1, 2), 1) AS d1,
CONVERT(DATE, date2, 112) AS d2,
-- ...
FROM yourTable
)
SELECT *
FROM casted
WHERE d1 <= DATEADD(YEAR, -1, d2)
try this
column1 is stored MMYYYY format and column2 is stored YYYYMMDD.
where column1= case when datepart(month,taskDuedate)>9 then convert(varchar,datepart(month,taskDuedate))else '0'+convert(varchar,datepart(month,taskDuedate))end+convert(varchar,datepart(year,taskDueDate))

DB2 Convert from YYYYMMDD to Date

I have a column that stores a date as char in the format 'YYYYMMDD'. Now I want to convert it to a real date.
I tried
select cast (DATEFIELD as DATE) as MyDate
But it only returns the old YYYYMMDD format labeled as 'DATE'. What am I doing wrong?
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as "MyDate"
Use the CONVERT function and the style 112 to get the output in YYYYMMDD
SELECT CONVERT(DATEFIELD, 112) as MyDate
FROM yourtable
Or style 100 for mon dd yyyy hh:mi
SELECT CONVERT(DATEFIELD, 100) as MyDate
FROM yourtable
Simply convert it.
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as MyDate
FROM <your_table>

retrieve day after tomorrow date query

I want to select day after tomorrow date in sql. Like I want to make a query which select date after two days. If I select today's date from calender(29-04-2015) then it should show date on other textbox as (01-05-2015). I want a query which retrieve day after tomorrow date. So far I have done in query is below:
SELECT VALUE_DATE FROM DLG_DEAL WHERE VALUE_DATE = GETDATE()+2
thanks in advance
Note that if you have a date field containing the time information, you will need to truncate the date part using DATEADD
dateadd(d, 0, datediff(d, 0, VALUE_DATE))
To compare 2 dates ignoring the date part you could just use DATEDIFF
SELECT VALUE_DATE FROM DLG_DEAL
WHERE datediff(d, VALUE_DATE, getdate()) = -2
or
SELECT VALUE_DATE FROM DLG_DEAL
WHERE datediff(d, getdate(), VALUE_DATE) = 2
Try like this:
SELECT VALUE_DATE
FROM DLG_DEAL WHERE VALUE_DATE = convert(varchar(11),(Getdate()+2),105)
SQL FIDDLE DEMO
SELECT VALUE_DATE FROM DLG_DEAL WHERE datediff(d, VALUE_DATE, getdate()) = -2
** I think you should try this**
SELECT DATEADD(day,2,VALUE_DATE) AS DayAfterTomorrow
FROM DLG_DEAL WHERE VALUE_DATE= GETDATE();
DATEADD(choiceToAdd, interval, date)
This function allows you to add or substract day,month, year,etc from date. In this interval is nothing but numeric value which you want to add or substract.

Check date between two dates in T-SQL

I have a stored procedure where want check that a date is between a fixed date and the current date/time (with GETDATE()):
SELECT
a, b
FROM myTbl
WHERE
DATE BETWEEN 'Day start datetime' AND GETDATE()
...for example :
WHERE
DATE BETWEEN '2013-09-10 00:00:00.00' AND 'GETDATE()'
How to do it?
A pair of DATEADD/DATEDIFF calls will round a date down to the previous midnight:
SELECT a , b
FROM myTbl
WHERE DATE BETWEEN DATEADD(day,DATEDIFF(day,0,GETDATE()),0) and GETDATE()
Alternatively, if you're on SQL Server 2008 or later:
SELECT a , b
FROM myTbl
WHERE DATE BETWEEN CONVERT(date,GETDATE()) and GETDATE()
'GETDATE()' is a string literal, GETDATE() is a T-SQL function.
Your query should look like:
SELECT a , b
FROM myTbl
WHERE DATE BETWEEN '2013-09-10 00:00:00.0' and GETDATE()
I think WHERE DATE BETWEEN '2013-09-10 00:00:00.00' and GETDATE() (without the single quotes around the GETDATE() call) should work just fine.
You can get Day start date time by converting Getdate() return value to a Date type and again to Datetime as below.
select a,b
from myTbl
where [date] between convert(datetime,convert([date], getdate()))
and getdate()

SQL BETWEEN Operator

Why am I getting '2009' data? What am i doing wrong with the WHERE Clause?
SELECT CONVERT(varchar, EventDate, 101) AS EVENTDATE,
CONVERT(varchar, ClosedDate, 101) AS CLOSEDDATED,
DATEDIFF(Day,EventDate,ClosedDate) AS DiffDate,
FROM mytable
WHERE (CONVERT(varchar, EventDate, 101) BETWEEN '04/01/2010' AND '04/30/2010')
You're doing a string comparison, which goes from left to right. '04/10/2009' is between '04/0' and '04/3'.
If the field you're comparing is a DATETIME, don't try to convert it. SQL server can convert the strings to dates and do the comparison properly.
If you use a supported date format, SQL Server will implicitly convert the string to a DATETIME:
SELECT CONVERT(varchar, EventDate, 101) AS EVENTDATE,
CONVERT(varchar, ClosedDate, 101) AS CLOSEDDATED,
DATEDIFF(Day,EventDate,ClosedDate) AS DiffDate,
FROM mytable
WHERE EventDate BETWEEN '2010-04-01' AND '2010-04-30'
Your query is just doing string comparison, which has no bearing on date spans.
Your WHERE clause may be doing string comparison instead of date comparison. If you want to do a date comparison you can change
CONVERT(varchar, EventDate, 101)
to
CAST (CONVERT(varchar, EventDate, 101) AS DATETIME)
You really don't need all the conversion. The dates from the calendar will have the right start and end times. You also want to consider events that might go past the end date or start before the date and end within the date range. Or finally start before and go past...
Here's some code we use
(EventStartDtTm >= startDt and EventStartDtTm <= endDt)
|| (EventStartDtTm <= startDt and EventEndDtTm >= startDt)
-- patrick