Incremental next month dates in SQL - sql

I have similar requirement, i have file feeding to SQL Server 2012 for a month but i wanted to convert the same file as a next month file.
For Example I have below
02/22/2018 02/23/2018 02/24/2018
and I want 03/22/2018 03/23/2018 02/24/2018
I am able to get upto this point but facing issue when i have below situation.
02/28/2018 02/24/2018
and I want 03/31/2018 03/31/2018
I have a normal calendar set up which I can use Can somebody please help.

you can use DATEADD() function for this like below :
SELECT DATEADD(month, 1, '02/22/2018') AS DateAdd;

I guess you are looking for EOMONTH function
somethink like this might help you solve your issue
DECLARE #date VARCHAR(10) = '02/28/2019'
SELECT CASE WHEN (EOMONTH(#date) = FORMAT(CAST(#date AS DATETIME),'yyyy-MM-dd'))
THEN EOMONTH(DATEADD(month,1,#date))
ELSE DATEADD(month,1,#date)
END
Note: I have used format function because EOMONTH returns date in 'yyyy-MM-dd' format
UPDATE
DECLARE #dates TABLE
(
FromDate DATETIME,
ToDate DATETIME
)
INSERT INTO #dates (FromDate)
VALUES('12/31/2017'),('1/31/2018'),('2/28/2018'),('3/31/2018')
select * from #dates
UPDATE #dates
SET ToDate =CASE WHEN (EOMONTH(FromDate) = FORMAT(CAST(FromDate AS DATETIME),'yyyy-MM-dd'))
THEN EOMONTH(DATEADD(month,1,FromDate))
ELSE DATEADD(month,1,FromDate)
END
SELECT * FROM #dates
Run this and you can see the final output

Related

SQL Server date conversion is 68 years behind the actual date

I want to convert an integer to a date format. I am using the following script:
SELECT CAST(7549 as datetime)
It returns me a date 1920-09-02
However, 7549 actual date is 1988-08-31
Can anyone please help me how to perform correct mapping?
See the reverse
DECLARE #YourDate AS Datetime
SET #YourDate = '1920-09-02'
SELECT CAST(#YourDate AS INT) --7549
SET #YourDate = '1988-08-31'
SELECT CAST(#YourDate AS INT) -- 32384
If you want the result as '1988-08-31 00:00:00.000', try with the following scripts.
SELECT CAST(7549 as datetime)-CAST('1832-01-03' as datetime)
OR
SELECT CAST(7549 as datetime)+CAST(24835 as datetime)

How to get month/day to compare with another month/day without year

I need to select month/day to compare with another month/day to determine which day is greater. For example, to compare 2/28 with 3/18. What would be the date format I can use to compare the days.
In SQL Server, you can convert dates to strings using the convert function:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
The format you want to use is mm/dd/yy, which is style 1 on the convert function. Then you can simply take the left 5 characters:
select left(convert(char(8),YourDate,1),5) as mm_dd
If you're only trying to display the month and day, you can use this:
DECLARE #myDate DATETIME2 = '02/28/2015'
SELECT CAST(MONTH(#myDate) AS VARCHAR(2)) + '/' + CAST(DAY(#myDate) AS VARCHAR(2))
SELECT LEFT(CONVERT(VARCHAR,#DATE,1),5)
You can use the format function?
select format(getdate(),'dd/MM')
Try using the DATEPART function to get the day of year for your dates.
DECLARE #StartDate DATETIME = '20150130';
DECLARE #EndDate DATETIME = '20141225';
SELECT
#StartDate AS StartDate,
DATEPART(DY, #StartDate) AS StartDy,
#EndDate AS EndDate,
DATEPART(DY, #EndDate) AS EndDy;

SQLServer remove Date part of datetime [duplicate]

How would I be able to extract the time part of a DateTime field in SQL? For my project I have to return data that has a timestamp of 5pm of a DateTime field no matter what the date is
This will return the time-Only
For SQL Server:
SELECT convert(varchar(8), getdate(), 108)
Explanation:
getDate() is giving current date and time.
108 is formatting/giving us the required portion i.e time in this case.
varchar(8) gives us the number of characters from that portion.
Like:
If you wrote varchar(7) there, it will give you 00:00:0
If you wrote varchar(6) there, it will give you 00:00:
If you wrote varchar(15) there, it will still give you 00:00:00 because it is giving output of just time portion.
SQLFiddle Demo
For MySQL:
SELECT DATE_FORMAT(NOW(), '%H:%i:%s')
SQLFiddle Demo
In SQL Server if you need only the hh:mi, you can use:
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
SELECT RIGHT('0'+CAST(DATEPART(hour, #datetime) as varchar(2)),2) + ':' +
RIGHT('0'+CAST(DATEPART(minute, #datetime)as varchar(2)),2)
If you want only the hour of your datetime, then you can use DATEPART() - SQL Server:
declare #dt datetime
set #dt = '2012-09-10 08:25:53'
select datepart(hour, #dt) -- returns 8
In SQL Server 2008+ you can CAST() as time:
declare #dt datetime
set #dt = '2012-09-10 08:25:53'
select CAST(#dt as time) -- returns 08:25:53
I know this is an old question, but since the other answers all
return strings (rather than datetimes),
rely on the internal representation of dates (conversion to float, int, and back) or
require SQL Server 2008 or beyond,
I thought I'd add a "pure" option which only requires datetime operations and works with SQL Server 2005+:
SELECT DATEADD(dd, -DATEDIFF(dd, 0, mydatetime), mydatetime)
This calculates the difference (in whole days) between date zero (1900-01-01) and the given date and then subtracts that number of days from the given date, thereby setting its date component to zero.
Try this in SQL Server 2008:
select *
from some_table t
where convert(time,t.some_datetime_column) = '5pm'
If you want take a random datetime value and adjust it so the time component is 5pm, then in SQL Server 2008 there are a number of ways. First you need start-of-day (e.g., 2011-09-30 00:00:00.000).
One technique that works for all versions of Microsoft SQL Server as well as all versions of Sybase is to use convert/3 to convert the datetime value to a varchar that lacks a time component and then back into a datetime value:
select convert(datetime,convert(varchar,current_timestamp,112),112)
The above gives you start-of-day for the current day.
In SQL Server 2008, though, you can say something like this:
select start_of_day = t.some_datetime_column
- convert(time, t.some_datetime_column ) ,
from some_table t
which is likely faster.
Once you have start-of-day, getting to 5pm is easy. Just add 17 hours to your start-of-day value:
select five_pm = dateadd(hour,17, t.some_datetime_column
- convert(time,t.some_datetime_column)
)
from some_table t
Note that from MS SQL 2012 onwards you can use FORMAT(value,'format')
e.g. WHERE FORMAT(YourDatetime,'HH:mm') = '17:00'
"For my project, I have to return data that has a timestamp of 5pm of a DateTime field, No matter what the date is."
So I think what you meant was that you needed the date, not the time. You can do something like this to get a date with 5:00 as the time:
SELECT CONVERT(VARCHAR(10), GetDate(), 110) + ' 05:00:00'
This should strip away the date part:
select convert(datetime,convert(float, getdate()) - convert(int,getdate())), getdate()
and return a datetime with a default date of 1900-01-01.
you can use CONVERT(TIME,GETDATE()) in this case:
INSERT INTO infoTbl
(itDate, itTime)
VALUES (GETDATE(),CONVERT(TIME,GETDATE()))
or if you want print it or return that time use like this:
DECLARE #dt TIME
SET #dt = CONVERT(TIME,GETDATE())
PRINT #dt
select cast(getdate() as time(0))
returns for example :- 15:19:43
replace getdate() with the date time you want to extract just time from!
SELECT DISTINCT
CONVERT(VARCHAR(17), A.SOURCE_DEPARTURE_TIME, 108)
FROM
CONSOLIDATED_LIST AS A
WHERE
CONVERT(VARCHAR(17), A.SOURCE_DEPARTURE_TIME, 108) BETWEEN '15:00:00' AND '15:45:00'
declare #datetime as datetime
set #datetime = getdate()
select cast(cast(#datetime as time) as varchar(8))
For year:
SELECT DATEPART(YEAR, '2021-03-21' );
For hour:
SELECT DATEPART(HOUR, '2021-03-21 08:50:30' );

Time part of a DateTime Field in SQL

How would I be able to extract the time part of a DateTime field in SQL? For my project I have to return data that has a timestamp of 5pm of a DateTime field no matter what the date is
This will return the time-Only
For SQL Server:
SELECT convert(varchar(8), getdate(), 108)
Explanation:
getDate() is giving current date and time.
108 is formatting/giving us the required portion i.e time in this case.
varchar(8) gives us the number of characters from that portion.
Like:
If you wrote varchar(7) there, it will give you 00:00:0
If you wrote varchar(6) there, it will give you 00:00:
If you wrote varchar(15) there, it will still give you 00:00:00 because it is giving output of just time portion.
SQLFiddle Demo
For MySQL:
SELECT DATE_FORMAT(NOW(), '%H:%i:%s')
SQLFiddle Demo
In SQL Server if you need only the hh:mi, you can use:
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
SELECT RIGHT('0'+CAST(DATEPART(hour, #datetime) as varchar(2)),2) + ':' +
RIGHT('0'+CAST(DATEPART(minute, #datetime)as varchar(2)),2)
If you want only the hour of your datetime, then you can use DATEPART() - SQL Server:
declare #dt datetime
set #dt = '2012-09-10 08:25:53'
select datepart(hour, #dt) -- returns 8
In SQL Server 2008+ you can CAST() as time:
declare #dt datetime
set #dt = '2012-09-10 08:25:53'
select CAST(#dt as time) -- returns 08:25:53
I know this is an old question, but since the other answers all
return strings (rather than datetimes),
rely on the internal representation of dates (conversion to float, int, and back) or
require SQL Server 2008 or beyond,
I thought I'd add a "pure" option which only requires datetime operations and works with SQL Server 2005+:
SELECT DATEADD(dd, -DATEDIFF(dd, 0, mydatetime), mydatetime)
This calculates the difference (in whole days) between date zero (1900-01-01) and the given date and then subtracts that number of days from the given date, thereby setting its date component to zero.
Try this in SQL Server 2008:
select *
from some_table t
where convert(time,t.some_datetime_column) = '5pm'
If you want take a random datetime value and adjust it so the time component is 5pm, then in SQL Server 2008 there are a number of ways. First you need start-of-day (e.g., 2011-09-30 00:00:00.000).
One technique that works for all versions of Microsoft SQL Server as well as all versions of Sybase is to use convert/3 to convert the datetime value to a varchar that lacks a time component and then back into a datetime value:
select convert(datetime,convert(varchar,current_timestamp,112),112)
The above gives you start-of-day for the current day.
In SQL Server 2008, though, you can say something like this:
select start_of_day = t.some_datetime_column
- convert(time, t.some_datetime_column ) ,
from some_table t
which is likely faster.
Once you have start-of-day, getting to 5pm is easy. Just add 17 hours to your start-of-day value:
select five_pm = dateadd(hour,17, t.some_datetime_column
- convert(time,t.some_datetime_column)
)
from some_table t
Note that from MS SQL 2012 onwards you can use FORMAT(value,'format')
e.g. WHERE FORMAT(YourDatetime,'HH:mm') = '17:00'
"For my project, I have to return data that has a timestamp of 5pm of a DateTime field, No matter what the date is."
So I think what you meant was that you needed the date, not the time. You can do something like this to get a date with 5:00 as the time:
SELECT CONVERT(VARCHAR(10), GetDate(), 110) + ' 05:00:00'
This should strip away the date part:
select convert(datetime,convert(float, getdate()) - convert(int,getdate())), getdate()
and return a datetime with a default date of 1900-01-01.
you can use CONVERT(TIME,GETDATE()) in this case:
INSERT INTO infoTbl
(itDate, itTime)
VALUES (GETDATE(),CONVERT(TIME,GETDATE()))
or if you want print it or return that time use like this:
DECLARE #dt TIME
SET #dt = CONVERT(TIME,GETDATE())
PRINT #dt
select cast(getdate() as time(0))
returns for example :- 15:19:43
replace getdate() with the date time you want to extract just time from!
SELECT DISTINCT
CONVERT(VARCHAR(17), A.SOURCE_DEPARTURE_TIME, 108)
FROM
CONSOLIDATED_LIST AS A
WHERE
CONVERT(VARCHAR(17), A.SOURCE_DEPARTURE_TIME, 108) BETWEEN '15:00:00' AND '15:45:00'
declare #datetime as datetime
set #datetime = getdate()
select cast(cast(#datetime as time) as varchar(8))
For year:
SELECT DATEPART(YEAR, '2021-03-21' );
For hour:
SELECT DATEPART(HOUR, '2021-03-21 08:50:30' );

Checking Date part only, of a DateTime field

I have a query like :
SELECT ..
FROM ...
WHERE ...
AND ISNULL(ICA.actual_appearance_date, ICA.scheduled_appearance_date)
BETWEEN LO.order_start_date
AND ISNULL(LO.actual_expiry_date, LO.expected_expiry_date)
Is there a neat way to only check that the actual_appearance_date is between the start and end date, but not include time?
So:
Appearance Date is 03/Oct/2011 # 14h00... and the dates we're check are 03/Oct/2011 # 15h00 and and 07/Oct/2011 # 10h00.
At the moment, that would not yield a result because of the time factors. I need to do a between on the date part only... So, between 03/Oct/2011 # 0h00 and really 08/Oct/2011 # 0:00.
Is there a neat way to do this?
Edit:
We're developing for a 2008 machine, but we're developing ON 2005 machines. Long story, but I can't use the nice and neat DATE fix.
I am trying the DATEADD method, but am finding an issue.
This is not returning the result I expect:
DECLARE #Table TABLE
(
StartDate DATETIME,
EndDate DATETIME
)
INSERT INTO #Table VALUES ('02-Jan-2011 13:00:00', '07-Jan-2011 15:30:00')
SELECT * FROM #Table
DECLARE #Date DATETIME
SET #Date = '07-Jan-2011 16:00:00'
SELECT
CASE WHEN
#Date BETWEEN DATEADD(dd,DATEDIFF(dd,0,StartDate),0) AND DATEADD(dd, DATEDIFF(dd,0,EndDate),0)-- must cover the appearance date or still open
THEN 1
ELSE 0
END AS Result
FROM #Table
Must I add +1 to the BETWEEN dates to include the last date? 'BETWEEN' doesn't seem to be inclusive of the last date..
You have SQL Server 2008 so just cast to date
SELECT ..
FROM ...
WHERE ... AND
CAST(ISNULL(ICA.actual_appearance_date, ICA.scheduled_appearance_date) AS date)
BETWEEN
CAST(LO.order_start_date AS date)
AND
CAST(ISNULL(LO.actual_expiry_date, LO.expected_expiry_date) AS date)
For SQL Server 2005 and earlier, use the DATEDIFF/DATEADD trick: Best approach to remove time part of datetime in SQL Server
Do not use varchar or float conversions
Edit:, for SQL Server 2005
You need to apply the DATEADD/DATEDIFF to all values
...
SELECT
CASE WHEN
DATEADD(dd,DATEDIFF(dd,0,#Date),0)
BETWEEN DATEADD(dd,DATEDIFF(dd,0,StartDate),0)
AND DATEADD(dd, DATEDIFF(dd,0,EndDate),0)
THEN 1
ELSE 0
END AS Result
FROM #Table
Or
WHERE ... AND
DATEADD(dd,DATEDIFF(dd,0,ISNULL(ICA.actual_appearance_date, ICA.scheduled_appearance_date),0)
BETWEEN
DATEADD(dd,DATEDIFF(dd,0,LO.order_start_date),0)
AND
DATEADD(dd,DATEDIFF(dd,0,ISNULL(LO.actual_expiry_date, LO.expected_expiry_date),0)
You can use the convert process then compare:
Convert(Datetime,Convert(Varchar,OrderDate,106),110)