MySQL - Can't Convert From MSSQL - mysql

Trying To convert This MSSQL 2012R2 Statement into MySQL 5.7
SELECT SUM(Cost + Credit_Amount) As Daily_Cost FROM Google_Costing
WHERE Date BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE() -2), 0) AND
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE() -2) + 1, 0)
I have tried replacing DATEADD with DATE_ADD and DATEDIFF with TIMESTAMPDIFF, but still no luck. Any help is appreciated.
I get the error:
Incorrect parameter count in the call to native function 'DATEDIFF'
So I change the statement to:
SELECT SUM(Cost + Credit_Amount) As Daily_Cost FROM Google_Costing
WHERE Date BETWEEN DATE_ADD(MONTH, TIMESTAMPDIFF(MONTH, 0, GETDATE() -2), 0) AND
DATE_ADD(MONTH, TIMESTAMPDIFF(MONTH, 0, GETDATE() -2) + 1, 0)
This time I get the error:
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use
near 'TIMESTAMPDIFF(MONTH, 0, GETDATE() -2), 0) AND DATE_ADD(MONTH,
TIMESTAMPDIFF(MONT' at line 2

Try out these biscuits...
SELECT SUM(Cost + Credit_Amount) As Daily_Cost FROM Google_Costing
WHERE Date BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01')
AND DATE_ADD(CURDATE(), INTERVAL -2 DAY)

Related

Evaluate Expression with expression builder

I am new to ssis and I would like to evaluate this using expression builder to get the current date in bigin. Any idea?
DATEPART(second, getdate()) +
DATEPART(minute, getdate()) * 100 +
DATEPART(hour, getdate()) * 10000 +
DATEPART(day, getdate()) * 1000000 +
DATEPART(month, getdate()) * 100000000 +
DATEPART(year, getdate()) * 10000000000
The error is
Expression cannot be evaluated.
------------------------------
ADDITIONAL INFORMATION:
The expression contains unrecognized token "second". If "second" is a variable, it should be expressed as "#second". The specified token is not valid. If the token is intended to be a variable name, it should be prefixed with the # symbol.
Attempt to parse the expression "DATEPART(second, getdate()) +
DATEPART(minute, getdate()) * 100 +
DATEPART(hour, getdate()) * 10000 +
DATEPART(day, getdate()) * 1000000 +
DATEPART(month, getdate()) * 100000000 +
DATEPART(year, getdate()) * 10000000000" failed and returned error code 0xC00470A4. The expression cannot be parsed. It might contain invalid elements or it might not be well-formed. There may also be an out-of-memory error.
(Microsoft.DataTransformationServices.Controls)
------------------------------
The syntax for DATEPART in an SSIS expression is different then the syntax in TSQL. Because that helps makes our lives harder than necessary.
The first argument, datepart, has to be enclosed in double quotes. This should get you closer to what you're after.
DATEPART("second", getdate()) +
DATEPART("minute", getdate()) * 100 +
DATEPART("hour", getdate()) * 10000 +
DATEPART("day", getdate()) * 1000000 +
DATEPART("month", getdate()) * 100000000 +
DATEPART("year", getdate()) * 10000000000
The next error you'll get is:
The literal "10000000000" is too large to fit into type DT_I4. The magnitude of the literal overflows the type.
You can fix that one by adding an L to the end of the last literal:
DATEPART("second", getdate()) +
DATEPART("minute", getdate()) * 100 +
DATEPART("hour", getdate()) * 10000 +
DATEPART("day", getdate()) * 1000000 +
DATEPART("month", getdate()) * 100000000 +
DATEPART("year", getdate()) * 10000000000L
And that evaluated correctly to 20200611143622.

How to convert MSSQL to MySQL date in PHP

I am a PHP developer and I am working in converting MSSQL to MySQL file while converting data's
CAST(0x063A0B00 AS Date)
I cant convert this as a timestamp.
SELECT
CAST(
'1900-01-01 00:00:00' +
INTERVAL CAST(CONV(substr(HEX(BinaryData),1,8), 16, 10) AS SIGNED) DAY +
INTERVAL CAST(CONV(substr(HEX(BinaryData),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND
AS DATETIME) AS converted_datetime
FROM
(
SELECT 0x0000987C00000000 AS BinaryData
UNION ALL
SELECT 0x00009E85013711EE AS BinaryData
) d
From how to cast the hexadecimal to varchar(datetime)?

data between two specific times in mysql get error

I am trying to select all data from my table where condition is
performDate will be between today 2.00 AM to tomorrow 2.00 AM
My query gives this error
Incorrect parameter count in the call to native function 'DATEDIFF'
My query is
SELECT * FROM `admin_marker` WHERE
FROM_UNIXTIME(performDate)
BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()+1), 0) + '02:00'
DATEDIFF expects only 2 parameters. You call it with 3 parameter.
Why dont you do it that way ?
WHERE performdate >= DATE_FORMAT(NOW(), '%Y-%m-%d 02:00:00') AND performdate <= DATE_FORMAT(CURRENTDATE + INTERVAL +1 DAY '%Y-%m-%d 02:00:00')

DATEDIFF() seconds with millisecond to float or decimal

How to convert the below code to float or decimal type?
SELECT DATEDIFF(ss, StartTime, GETDATE()) + '.' +
DATEDIFF(ms, StartTime, GETDATE())
This is what I needed. CONVERT(float, DATEDIFF(ms, StartTime, GETDATE()) / 1000.0)
The code i my question was completely wrong because DATEDIFF(ms, StartTime, GETDATE()) returns total number of milisecond between the two dates and not as i thought only difference in the milliseconds part.
The code im my question would work if I used the DATEPART instead DATEDIFF in both expressions:
DATEPART(ss, GETDATE()) - DATEPART(ss, #StartTime) + '.'
+ DATEPART(ms, GETDATE()) - DATEPART(ms, #StartTime)

Getting the Starting and ending date of week? In Sql server?

In sql, how to get the starting and ending date of week for the particular month. Can you help me.
The following will work whatever you consider the first day of the week (sunday, monday etc), just ensure you use SET DATEFIRST if you want to change from the default. SET DATEFIRST 1 will make the first day of the week monday.
SELECT DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekStart],
DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekEnd]
EDIT
I have just re-read you request and I think you may be after something different to what I have given above. If you want the day of the week of the first and last of the month this will do the trick:
SELECT DATENAME(WEEKDAY, DATEADD(DAY, 1 - DATEPART(DAY, GETDATE()), GETDATE())) [FirstDayOfMonth],
DATENAME(WEEKDAY, DATEADD(DAY, - DATEPART(DAY, DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))) [LastDayOfMonth]
Your requirement is unclear: what do you consider to be the first and last days of a week? Sunday and Saturday? Monday and Friday? Monday and Sunday? But the best solution for many date-related queries is to create a calendar table. That gives you the ability to easily set the first and last days of weeks, months and years according to your definition and even in multiple formats if necessary.
This is especially useful if you work with concepts like financial years, business days, and so on where the definitions vary by country and even company. It is also the easiest solution when there are exceptions to your logic, e.g. the first week of the year has different rules than a 'normal' week.
With a calendar table you could pre-populate the first and last days of the week and then your query could be something like this:
-- first/last days stored as dates in their own columns
select FirstDayOfThisWeek, LastDayOfThisWeek
from dbo.Calendar
where BaseDate = #SomeDate
-- first/last days stored as bit flags
select max(BaseDate)
from dbo.Calendar
where BaseDate <= #SomeDate and IsFirstDayOfWeek = 0x1
Below is a case statement you can use to create a Weekbegin for your date value. It will make your weeks start on Monday or Tuesday or Wed... etc. dependant on what day of the week you have set as #pDate.
Create parameters
DECLARE #pDate Date = NULL --Replace with your date, which will also be the first day of the week
DECLARE #pDatePart SMALLINT = DATEPART(dw, #pDate)
Then put this case Statement after your select
CASE
WHEN DATEPART(dw, CAST(DATEVALUE AS DATE)) BETWEEN #pDatePart AND 7 THEN DATEADD(d, (DATEPART(dw, CAST(DATEVALUE AS DATE))*-1)+#pDatePart, CAST(DATEVALUE AS DATE))
WHEN DATEPART(dw, CAST(DATEVALUE AS DATE)) BETWEEN 1 AND #pDatePart-1 THEN DATEADD(d, (DATEPART(dw, CAST(DATEVALUE AS DATE))*-1)+(#pDatePart-7), CAST(DATEVALUE AS DATE))
END
AS DynamicWeekBegin
You can always get the final date of the week, just by using dateadd(d, 6, THE CASE STATEMENT)
Here i given
Week,month,quarter,half year,year start and end date.
Select CONVERT(varchar(50), GETDATE(),105) 'GETDATE' ,
CONVERT(varchar(50), DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)),105) [WeekStart],
CONVERT(varchar(50),DATEADD(DAY, 8 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) ,105)[WeekEnd],
CONVERT(varchar(50),DATEADD(dd, -DAY(getdate()) + 1, getdate()),105) MonthStart,
CONVERT(varchar(50),DATEADD(dd, -DAY(DATEADD(mm, 1, getdate())), DATEADD(mm, 1, getdate())),105) MonthStart,
CONVERT(varchar(50), DATEADD(q, DATEDIFF(q, 0, GETDATE()), 0),105) AS 'QStart Date',
CONVERT(varchar(50), DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, GETDATE()) + 1,0)),105) AS 'QEnd Date',
CONVERT(varchar(50), CAST(CAST(((((MONTH(GETDATE()) - 1) / 6) * 6) + 1) AS VARCHAR) + '-1-' + CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME),105) StartOfHalfYear,
CONVERT(varchar(50), CAST(CAST(((((MONTH(GETDATE()) - 1) / 6) * 6) + 6) AS VARCHAR) + '-1-' + CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME),105)EndOfHalfYear,
CONVERT(varchar(50), DATEADD(yy, DATEDIFF(yy,0,getdate()), 0),105) AS StartOfYear,
CONVERT(varchar(50), DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1),105) AS EndOfYear