I need to write a function which will return me the previous month and year.
Like if the input is : function_name('February', 2015) the output will be 'January', 2014.
In SQL Server 2008 there is the date data type, which has no time attached. You can thus remove the time portion quite easily simply by converting, then performing the DateAdd.
SELECT DateAdd(month, -1, Convert(date, GetDate()));
This will return a date data type. To force it to be datetime again, you can simply add one more Convert:
SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
You may not need the explicit conversion to datetime, though.
Related
I was asked to create an SSRS Report, but taking the live sales of today and compare it to sales the exact same time as a date to be named later. For example, "How are we doing compared to this time on last Saturday?" The user would simply pick the date, and the query would calculate sales for both Today() and #BusinessDate, from the start of the business until Today()'s time.
I am sure I can do the query, but I don't know how to set a query parameter for this.
Please note, Today() and Now() throw errors, but GETDATE() works fine.
Got it:
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), #BusinessDate, 112) + ' ' + CONVERT(CHAR(8), GETDATE(), 108))
How can I get year and month values only as yyyy-mm format from datetime field?
I tried
Select EXTRACT(YEAR_MONTH From task_completion) from task;
But this results in 201901 format.
Also I tried this solution from this post:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, task_completion), 0)
FROM task
Which gives Incorrect parameter count in the call to native function 'DATEDIFF' error, since this was solution for sql-server.
Is there any equivalent solution for mysql?
year and month function in mysql
select year(task_completion),month(task_completion) from task
but if you need year with month use DATE_FORMAT
select DATE_FORMAT(task_completion,'%Y-%m') from task
SELECT CONCAT(year(task_completion), '-' ,month(task_completion)) FROM task
If I use this in my dataset SQL:
DECLARE #StartDate DATE = '2018-01-01'
DECLARE #EndDate DATE = '2018-03-01'
The report runs and returns the expected Data.
When I comment out the #StartDate & #EndDate variables - delete the two parameters from the Report - and run the report using Date Prompts at run time [using the same Dates] - I get no data returned.
The Date field that I am attempting to filter on is a Datetime field.
I have tried the following two approaches in my SQL:
o.ORDERDATE >= #StartDate And o.ORDERDATE <= #EndDate
cast(o.ORDERDATE as DATE) >= Cast(#StartDate As Date) And cast(o.ORDERDATE as DATE) <= Cast(#EndDate As Date)
No data.
I added a Text Box to the Report Header and put the #StartDate Parameter value in there and got this: 01/01/2018 12:00:00 AM at run time.
I have gone back and forth a few times between uncommenting and commenting the Date variables at the top of the SQL. When I use the local variables - I get data. When I use the Date Parameters - no data.
I use Date Prompts in many of my other reports with no problems. I will go back and see if any of them is on this particular table and this particular date field - or if any of the other dates are Datetime fields ....
Meanwhile, I would appreciate any suggestions.
Thanks!
I would suspect a conversion issue comparing Date and DateTime types. To prove this, set the dates manually in your query to the full date and time. if this produces no results then try something simple like.
WHERE CAST(o.ORDERDATE AS Date) Between #StartDate AND #EndDate
Bear in mind that if you start your date with the year (I think) by default this is interpreted as being in YYYY-MM-DD format.
I have a table which stores people's orders, and within this table is a DateTime field. How can I get the information through a query, which includes only today's Date, without needing the time?
WHERE CONVERT(DATE, YourDateField) = CONVERT(DATE, GETDATE())
As you didn't add a RDBMS - this solution works for MS SQL Server:
WHERE DATE(yourdatefield) = Convert(date, getdate())
The conversion of a date data type to a datetime data type resulted in an
out-of-range value.
The statement has been terminated.
I just need to convert a date field to a date time format.
Example of what the code would look like to convert from date to datetime2
DECLARE #d1 date;
SET #d1 = GETDATE()
-- When converting date to datetime the minutes portion becomes zero.
SELECT CAST (#d1 AS datetime2) AS [date as datetime2]
For more information about cast and convert, see the Microsoft reference.
You probably get out of range exception because Date supports a date range between
January 1, 1 and December 31, 9999 , while DateTime supports a date range only between
January 1, 1753 to December 31, 9999.
Want to know Why 1753? Read this. (Recommended reading for anyone that likes trivia items)
Try converting to Datetime2 instead of Datetime, this should be OK since Datetime2 supports a date range similar to date.
the conversion can be done simply by using CAST:
SELECT CAST(YourDateTypeColumn As Datetime2)
FROM YourTable