DATEDIFF() seconds with millisecond to float or decimal - sql-server-2008

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)

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.

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')

How to replace a date with a standard date in SQL

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

SSIS expression to find previous Friday

Using SQL Server 2008, I have built an SSIS variable with dynamic value base on current date. I would like to have its value to be Friday if the current day is Monday, and here is the expression built:
DATEPART("dw",GETDATE()) != 2?
RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD("dd", -1, GETDATE())), 2) + "/"
+ RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("dd", -1, GETDATE())), 2) + "/" +
(DT_WSTR,4)YEAR(DATEADD("dd", -1, GETDATE())) : RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD("dd", -1, GETDATE())), 2) + "/"
+ RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("dd", -3, GETDATE())), 2) + "/" +
(DT_WSTR,4)YEAR(DATEADD("dd", -1, GETDATE()))
Issue: it is not accurate when the month or year changed. Is there any better way to do this? Help would be appreciated. Thanks.
Will this work (you can substitute GETDATE() for #date, I just used that to easily test out different dates)
DECLARE #date DATETIME
SET #date = '2013-01-14'
SELECT
PrevFriday = CASE WHEN DATEPART(weekday, #date) <> 2 THEN #date
ELSE DATEADD(DAY, -3, #date)
END
UPDATE: Here is same, but done in SSIS Variable Expression:
DATEPART("dw", GETDATE()) != 2?
GETDATE():
DATEADD("dw", -3, GETDATE())
UPDATE #2: Here is how to return the previous Friday for ANY date, not just Monday
SELECT DATEADD(DAY, -1 - (DATEPART(weekday, #date) % 7), #date)

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