Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific day of current month and year

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.

like image 492
Sk1X1 Avatar asked Feb 28 '13 17:02

Sk1X1


3 Answers

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
like image 72
Aaron Bertrand Avatar answered Oct 25 '22 13:10

Aaron Bertrand


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)
like image 21
Lamak Avatar answered Oct 25 '22 13:10

Lamak


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

like image 45
Joe Johnston Avatar answered Oct 25 '22 13:10

Joe Johnston