If today's date is greater than 10, then it will show like this:
Today = 20170611
Result = 20170710M1231
I tried this
CASE
WHEN (SUBSTRING(CONVERT(VARCHAR(6), GETDATE(), 112), 4, 2)) > 10
THEN (DATEADD(MONTH, 1, (SUBSTRING(CONVERT(VARCHAR(6), GETDATE(), 112), 1, 6)))) + '10M1231'
ELSE ''
END AS FREQUENCY
but I couldn't get the desired answer
You were adding the month after spliting the date,this should work
SELECT CASE WHEN DAY(GETDATE()) > 10
THEN (SUBSTRING(CONVERT(VARCHAR(6),DATEADD(MONTH,1,GETDATE()),112),1,6))+'10M1231'
ELSE '' END AS FREQUENCY
Related
Just wondering if anyone can help me setting a date variable for a report to run between certain dates every year without needing to be updated manually.
For example, I have the dates hard-coded below, but I was wondering if I could set a date format for when it goes into 2017 that the dates will change for the 2017 year. I presume there is a way to set a yearly date format I am just not sure how.
This is my hard-coded variables I set.
Put in the wrong Date Ranges changed as below.
set #start_date = '2016-01-02';
set #end_date = '2017-01-01';
Below is what I have in my where clause also.
and create_date between #start_date and #end_date
Your range covers all days in the year, except January 1. So you can simply check the day and month:
WHERE NOT (DAY(create_date) = 1 AND MONTH(create_date) = 1)
set #start_date = CAST(CAST(YEAR(getdate()) AS nvarchar(4)) + '0102' AS DATETIME);
to get end of current year
SET #end_date = DATEADD(day, -1 , DATEADD(month,13 - MONTH(getdate()), DATEADD(day,1 - DAY(getdate()),getdate())));
I assume you want the FULL "current year" based on "today"
MySQL
select *
from whatever
where ( create_date >= CONCAT(YEAR(CURDATE()),'-01-01')
and create_date < CONCAT(YEAR(CURDATE())+1,'-01-01')
)
Test it with:
SELECT
CONCAT(YEAR(CURDATE()),'-01-01') This_year_start
, CONCAT(YEAR(CURDATE())+1,'-01-01') Next_year_start
SQL Server (tsql)
select *
from whatever
where ( create_date >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
and create_date < DATEADD(YEAR, DATEDIFF(YEAR, -1, GETDATE()), 0)
)
Test it with:
select
DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) , 0) This_year_start
, DATEADD(YEAR, DATEDIFF(YEAR, -1, GETDATE()), 0) Next_year_start
This_year_start Next_year_start
01.01.2016 00:00:00 01.01.2017 00:00:00
DATEDIFF(YEAR, 0, GETDATE()) calculates the number of years from the
base year
add that number of years to the base date (1900-01-01)
DATEDIFF(YEAR, -1, GETDATE()) calculates the number of years from
year before the base year (is a bigger number, by 1, than above)
add that number of years to the base date (1900-01-01)
Please avoid using between for date ranges. It is far safer to use a combination of >= and <
e.g.
where create_date >= '2016-01-01' and create_date < '2017-01-01'
With that approach, no matter what time precsion applies to the data in [create_date] you will get the precise range of information requested.
Here you go.
declare #s datetime, #e datetime
set #s=cast(cast(year(getdate()) as char(4))+'-01-02' as date)
set #e=cast(cast(year(getdate()) as char(4))+'-12-31' as date)
select #s,#e
I have sql query, where if the difference between 2 times is greater than 1440, then it will divide by 7, if not it will return the actual value. The problem is that I am repeating the code in the return value.
SELECT
'IF(TIMESTAMPDIFF(MINUTE, now(), end_at) > 1440, TIMESTAMPDIFF(MINUTE, now(), end_at)/ 7, TIMESTAMPDIFF(MINUTE, now(), end_at)) as minutesLeft'
FROM table
How can I replace the repeating code to look like this?
SELECT
'IF(TIMESTAMPDIFF(MINUTE, now(), end_at) > 1440, timestamp / 7, timestamp ) as minutesLeft'
FROM table
You can try using in-statement assignments:
set #diff = null;
SELECT #diff := TIMESTAMPDIFF(MINUTE, now(), end_at),
IF( #diff > 1440, #diff / 7, #diff) as minutesLeft
FROM table
Wrap the select in a sub-query if you just want one column returned.
Ok,
This is a little tricky, I am trying to replace the dates in a SQL Query results with a standard date, based on the month.
For example:
Any dates that are in July get 20140701
August gets 20140801
I could use a case statement:
Case
When Datepart(mm, TxnDate) = 1 and Datepart(yy, TxnDate) = 2014 then TxnDate = 20140101
etc...
but that could get very long as the database goes back 5 years and the result sets cover different periods then.
Any quick suggestions would be greatly appreciated.
Thanks,
declare #mydate datetime
select #mydate = GETDATE()
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
select #mydate = GETDATE() - 10
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
should print 20140701 and 20140601
If you simply want to set every TxnDate to the first of its month, you can do this:
TxnDate = DATEADD(month, DATEDIFF(month, 0, TxnDate), 0)
If your requirement is more complicated than that, you will need to explain.
Try Something like this
SELECT REPLACE (CONVERT(VARCHAR(8), TxnDate, 112),SUBSTRING(CONVERT(VARCHAR(8), TxnDate, 112),7,8),'01') AS [YYYYMMDD]
hope this is what you are looking for
Could any one help me convert this Microsoft access query to Tsql 2008:
SELECT
dbo_Invoiceable__c.NAME AS Invoiceable,
dbo_Invoiceable__c.Payment_is_due__c,
dbo_Invoiceable__c.Day_Due__c,
DATE () AS [Invoice Date],
DateSerial(Year(DATE ()), Month(DATE ()), [Day_Due__c])
AS [On a day of the month],
DateAdd("d", [day_due__c], DATE ())
AS [In a given number of days],
DateAdd("d", [day_due__c] - 1, DateAdd("m", 1, DateSerial(Year(DATE ()), Month(DATE ()), 1)))
AS [No of days after EOM],
IIf([Payment_is_due__c] = "In a given number of days", [In a given number of days], "") & IIf([Payment_is_due__c] = "On a day of this month", [On a day of the month], "") & IIf([Payment_is_due__c] = "No of days after EOM", [No of days after EOM], "")
AS [Due Date]
FROM dbo_Invoiceable__c;
I have tried this:
SELECT *,
CASE
WHEN [Payment_is_due__c] = 'In a given number of days' THEN [In a given number of days] ELSE ''
END + CASE
WHEN [Payment_is_due__c] = 'On a day of this month' THEN [On a day of the month] ELSE ''
END + CASE
WHEN [Payment_is_due__c] = 'No of days after EOM' THEN [No of days after EOM] ELSE ''
END AS [Due Date]
FROM (SELECT [SalesForce_LIVE.dbo.Invoiceable__c].[Name] AS Invoiceable,
[SalesForce_LIVE.dbo.Invoiceable__c].[Payment_is_due__c],
[SalesForce_LIVE.dbo.Invoiceable__c].[Day_Due__c],
CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1) AS [Invoice Date],
dbo.DateSerial(Year(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), Month(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), [Day_Due__c]) AS [On a day of the month],
DATEADD(d, [day_due__c], CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)) AS [In a given number of days],
DATEADD(d, [day_due__c] - 1, DATEADD(m, 1, dbo.DateSerial(Year(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), Month(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), 1))) AS [No of days after EOM]
FROM SalesForce_LIVE.dbo.Invoiceable__c) AS Nested1;
But still cant get the dateserial working in Tsql
DECLARE #firstdayofmonth datetime;
DECLARE #lastdayofmonth datetime;
SET #firstdayofmonth = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0);
SET #lastdayofmonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0));
SELECT *,
CASE WHEN [Payment_is_due__c]='In a given number of days' THEN [In a given number of days] ELSE '' END +
CASE WHEN [Payment_is_due__c]='On a day of this month' THEN [On a day of the month] ELSE '' END +
CASE WHEN [Payment_is_due__c]='No of days after EOM' THEN [No of days after EOM] ELSE '' END AS [Due Date]
FROM (SELECT SalesForce_LIVE.dbo.Invoiceable__c.[Name] AS Invoiceable, SalesForce_LIVE.dbo.Invoiceable__c.[Payment_is_due__c], SalesForce_LIVE.dbo.Invoiceable__c.[Day_Due__c],
CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),1),1) AS [Invoice Date],
DATEADD(DAY,[day_due__c],#firstdayofmonth-1) AS [On a day of the month],
DATEADD(DAYOFYEAR,[day_due__c],CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),1),1)) AS [In a given number of days],
DATEADD(DAY,[day_due__c],#lastdayofmonth) AS [No of days after EOM]
FROM SalesForce_LIVE.dbo.Invoiceable__c
) AS Nested1
I am trying to compare a date against the first of next month from last year. If the date from date1 is less than the first of next month from last year then I want it to return true. The condition only seems to fail if I set the date to one year ahead.
LastRightToKnow = 7/14/2011
Dim RTK As String
RTK = ""
If [NeedsRightToKnow] = -1 And [LastRightToKnow] < DateSerial(Year(Now() - 1), Month(Now() + 1), 1) Then
RTK = "Right-To-Know"
End If
So, from my understanding, I gather that if today = 6/14/2012 then
DateSerial(Year(Now() - 1), Month(Now() + 1), 1) = 7/01/2011
LastRightToKnow = 7/14/2011
therefore 7/14/2011 < 7/01/2011 should return False. However it returns true... What am I missing?
Your formula is off because of the placement of the parethesis:
? DateSerial(Year(Now() - 1), Month(Now() + 1), 1)
6/1/2012
? DateSerial(Year(Now()) - 1, Month(Now()) + 1, 1)
7/1/2011
Edit: need to do the same for month
Now() +1 = tomorrow
I think you mean
DateSerial(Year(Now())-1, Month(Now())-1, 1) = 7/01/2011