Specific day of current month and year - sql

I have problem with return of specific day of current month and year. I need for example 15th day. Until now I used in FB/IB existing function:
IB_EncodeDate(EXTRACT(YEAR FROM Current_Date),EXTRACT(Month FROM Current_Date),15)
Does it exist a simply way to convert this for MSSQL database?
edit. I need output in OLE format (41,348 by example) to compare date with another date. I compare date from database with 15th day of current month.

For the 15th day of current month:
SELECT DATEADD(DAY, 14, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0));
To get the silly OLE representation based on this "magic" date, 1899-12-30:
SELECT DATEDIFF(DAY, -2, DATEADD(DAY, 14,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)));
Answer (on March 11th, when I updated this answer for the changed requirement):
-----
41348

So, you have a date, and want to return the 15th day of the same month?. Well, assuming SQL Server 2008, you could do this:
SELECT CONVERT(DATE,CONVERT(VARCHAR(6),GETDATE(),112)+'15',112)
For Previous versions of SQL Server:
SELECT CONVERT(DATETIME,CONVERT(VARCHAR(6),GETDATE(),112)+'15',112)

This seems like a quick answer.
declare #OLEDate int
declare #currentDate as datetime
set #currentDate = DATEADD(DAY, 14, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
set #OLEDate = convert(float, #currentdate)
-- PRINT #OLEDate
based on Aaron Bertrand's answer and your need for the integer conversion

To get 10th day of current day
declare #cur_month int,#cur_yr int,#tenth_dt date
set #cur_month=month(getdate())
set #cur_yr=YEAR(getdate())
set #tenth_dt=convert(date,'10/'+convert(varchar(5),#cur_month)+'/'+convert(varchar(5),#cur_yr),103)
select #tenth_dt

Not sure if you after Day or Date. This gives both dayOfWeek and specificDate for any culture
declare #myDay int = 15
select convert(date,myday) specificDate, datename(dw,myday) dayOfWeek
from (
select convert(varchar(6),getdate(),112) + convert(varchar, #myDay) myday
) x
Fiddle Demo Here
| SPECIFICDATE | DAYOFWEEK |
----------------------------
| 2013-02-15 | Friday |

Current_Date in SQL Server would be getdate().
To get the 15th day in OLE Automation format, try:
select datediff(day, '18991230', dateadd(day, -day(getdate()) + 15, getdate()))

A bit more straightforward approach:
CAST(FORMAT(GETDATE(), 'yyyy-MM-15') AS DateTime)

Related

date format : get the last and the first day in the month-sql-DB2

how i can write the following date with date function.. IN DB2
L2.FOM >= '2015-08-01' // the first day of the current Month
L2.TOM <= '2015-08-31' // the last day of the current Month
i tried this but it doesnot work
L2.FOM=DATEADD(month,DATEDIFF(month,0,CURRENT DATE), 0) //for the first day of the month
last date
DECLARE #date DATETIME = '12/1/2011';
SELECT EOMONTH ( #date ) AS Result;
GO
first date - is known to start from 1.
but here it is for name sake.
SELECT DATEADD(month, DATEDIFF(month, 0, #date), 0) AS StartOfMonth
Your code looks like SQL Server.
In that database, I would suggest using eomonth():
select eomonth(getdate()) as last_date_of_month,
dateadd(day, 1, eomonth(getdate(), -1)) as first_date_of_month
For the first date of the month, I also often use:
datefromparts(year(getdate()), month(getdate()), 1)

Convert day number to full date in SQL Server

I searched a lot online and could not find it. I found just the opposite - date to day
For example: Day 221 ==> 09/08/2017
This query do from this date to day number:
SELECT DATEPART(DAYOFYEAR, SYSDATETIME())
or
SELECT DATEDIFF(day, CAST(DATEPART(YEAR, GETDATE()) AS CHAR(4)) + '-01-01', GETDATE() + 1) AS number_of_today
Thanks
Yet another option is with DateFromParts() and GetDate() for the current year
Example
Select DateAdd(DAY,221,DateFromParts(Year(GetDate())-1,12,31))
Returns
2017-08-09
This will give you the date as it relates to January 1st, 1900:
SELECT
dateadd(day, dayToConvert, 0)
FROM
myTable
I think you can change it to base off of January 1st, 2017 by:
SELECT
dateadd(day, dayToConvert, '2016-12-31')
FROM
myTable
It is December 31st because you want to add that many days to the date in the third argument.
The DATEADD function should work, as so:
SELECT DATEADD(day,221,someDate) AS someDate2 FROM sometable
Docs here
Tutorial here
Stored Procedure Option:
CREATE PROCEDURE PROC_NUM_TO_DATE #NUM INT,#DATESTART DATE AS
BEGIN
SELECT DATEADD(DAY,#NUM ,DATEADD(Day,-1,#DATESTART))
END
GO
EXECUTE:
EXEC PROC_NUM_TO_DATE 221,'2017-01-01'
RESULT:
2017-08-09

Get Last Day of the Month with the time set to 00:00:00 on MSSQL

I had a query and I am trying to get the yesterday date at 00:00:00 and the previous month date at 00:00:00.
This is my query:
SELECT DATEADD( DD,-2, CONVERT( CHAR(8) , getdate() , 112 )) 'Yesterday',
CONVERT( CHAR(8) ,DATEADD(ms,-3, DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) , 112 ) 'Last Day of Last Month'
The result is as follows:
Yesterday:2015-05-24 00:00:00.000
Last Day of Last Month:20150430
What is wrong here? Even if I move the convert I still getting this format. Is my approach correct?
Try this:
select
dateadd(dd, datediff(dd, 0, getdate()) - 1, 0) as yesterday,
dateadd(dd, -1, dateadd(mm, datediff(mm, 0, getdate()), 0)) as lastDayOfLastMonth
For more common date routines, visit this article by Lynn Pettis
If you're using SQL Server 2012 or above, you can simply use EOMONTH() built-in function.
SELECT CAST(EOMONTH (CURRENT_TIMESTAMP, -1) AS DATETIME) will return you 2015-04-30 00:00:00.000

How to calculate a date after 1 month currentDate

How can I calculate a date after a month of current date.
Example if I have 19/04/2012 I want to get 19/05/2012. Is there any function on sql that allows doing this?
I need to do the same for a year also like 19/04/2012 and set 19/04/2013
Cheers
You can use
DATEADD (datepart , number , date )
as specified here
Examples (thanks to DarrenDavis)
for month:
select dateadd(m, 1, getdate())
for year:
select dateadd(YY, 1, getdate())
Use the dateadd() function
SELECT dateadd(mm, 1, '19-Apr-2012')
To add 1 month to the current date:
SELECT DATEADD(m, 1, GETDATE())
To add 1 year to the current date:
SELECT DATEADD(YY, 1, GETDATE())
For additional arguments see:
http://msdn.microsoft.com/en-us/library/ms186819.aspx
Something like this should do the job
SELECT DATEADD(month, 1, '2012-04-19')
for a year
SELECT DATEADD(year, 1, '2012-04-19')
http://msdn.microsoft.com/en-us/library/ms186819.aspx
You can use DateAdd:
select dateadd(m, 1, getdate())
select dateadd(yy, 1, getdate())
search online for other period indicators such as hour, minute, etc!
You can use this query for 1 month ahead date . This query works fine in oracle.
select add_months(trunc(sysdate),1) from dual;
For 1 year ahead date use this query
select add_months(trunc(sysdate),12) from dual;

SQL query to find last day of current month?

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
LastDay_CurrentMonth
Hi everyone I have a query to find the last day of current month, which is surely working well, but I am unable to understand it, because I have other similar requirements and have to change it accordingly.
Can somebody explain it to me..
Thanks in advance
Get the DateTime of Now
GETDATE() -- 2011-09-15 13:45:00.923
Calculate the difference in month's from '1900-01-01'
DATEDIFF(m, 0, GETDATE()) -- 1340
Add the difference to '1900-01-01' plus one extra month
DATEADD(m, DATEDIFF(m, 0, GETDATE())+1, 0) -- 2011-10-01 00:00:00.000
Remove one second
DATEADD(s, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE())+1, 0)) -- 2011-09-30 23:59:59.000
This will give you the last day of current month but ignores time
select EOMONTH(GETDATE())
From Microsoft tech net
CREATE FUNCTION EOMONTH
(
#date datetime,
#months int
)
RETURNS datetime
AS
BEGIN
declare #eom datetime
declare #d datetime
set #d = dateadd(MONTH, #months, #date)
select #eom = dateadd(SECOND,-1,DATEADD(MONTH,datediff(MONTH,0,#d)+1,0))
RETURN #eom
END
GO
SELECT DATEPART (DD,EOMONTH(GETDATE())) AS 'the last day of the Current month'
Here DATEPART function is used to print the integer part of day.. if the the name of day is asked then we have to use DATENAME() function.
In query, last day means end of the month so we used EOMONTH() function.
GETDATE() is to represent current month.