Can Some One help me with this please. I was trying to use this Excel condition in SQL but I am getting error.
EXCEL Condition = "=DATE(2017,MONTH(EOMONTH(B2,0)+1),DAY(EOMONTH(B2,0)+1))"
SQL Query = DATEFROMPARTS(YEAR(GETDATE()),CONVERT(Varchar,
(MONTH(EOMONTH(hiredate,0)+1)),120),CONVERT(Varchar,
(DAY(EOMONTH(HireDate,0)+1)),120)) as 'DATE'
ERROR: Msg 206, Level 16, State 2, Line 1
Operand type clash: date is incompatible with int
Thank You in Advance
In Your Query Use DATEADD() Function OF SQL As Shown As Below,This May Work For You.
Use
CONVERT(Varchar,MONTH(DATEADD(dd,1,EOMONTH(GETDATE(),0))),120)
Instead Of
(MONTH(EOMONTH(hiredate,0)+1))
I guess you're trying to get the first day of the month after hiredate, but I can't tell for sure.
This MySQL expression generates that, using the handy-dandy LAST_DAY() function.
LAST_DAY(hiredate) + INTERVAL 1 DAY
If you want the first day of the month in which hiredate occurs, use this.
LAST_DAY(hiredate) + INTERVAL 1 DAY - INTERVAL 1 MONTH
If you're working with SQL Server (the Microsoft product) this expression gets you the first day of the month in which hiredate occurs.
DATEFROMPARTS(YEAR(hiredate), MONTH(hiredate), 1)
This gets you the first day of the next month.
DATEADD(MONTH, 1,DATEFROMPARTS(YEAR(hiredate), MONTH(hiredate), 1))
As you can see, it's different in SQL Server and MySQL. Job security for data base hackers. The built-in data handling functions deal correctly with stuff like 28, 29, 30, and 31-day months, and leap years, and all those calendar minutiae, so you don't have to.
Related
I have no experience working with DB2 before and I'm kind of stuck in something. I'm working on a project in SSIS reading from DB2 where I write into a flat file. I need to run the process weekly and get data from past 7 days.
My query works this way:
Select * From Table
Where ServiceDate >= 2200624 - 7
The above query brings data from the past 7 days, but this query don't work for me since I need to execute this process weekly. I need something like this:
Select * From Table
Where ServiceDate >= DATE(CURRENT_DATE - 7 DAY)
The second query throws an error, is there any other way to achieve this? I'm using ODBC source and I was thinking to use a dynamic query in SSIS but I'm not sure how this works in ODBC source, any suggestions or help will be appreciated.
EDIT:
This tables were created a long time ago, so I don't have any information about the data type of these tables.
The actual date 2200624 correspond to 20200624. This is the way that my date shows in the table.
Thanks in advance
For ServiceDate as YYYYMMDD INT:
Select * From Table
Where ServiceDate >= INT(TO_CHAR(CURRENT_DATE - 7 DAY, 'YYYYMMDD'));
If ServiceDate is CHAR(7) or equivalent, and if value 2200624 corresponds to YYYYMMDD date 20200624 as per your edited question, then the following examples might help.
It assumes ServiceDate values beginning with first character 1 are in the 20th century (19xx years), and dates with first character 2 are in the 21st century.
SELECT ... FROM ... WHERE ( TO_DATE(CASE SUBSTR(ServiceDate,1,1) WHEN '1' THEN '19'||SUBSTR(ServiceDate,2,6) WHEN '2' THEN '20'||SUBSTR(ServiceDate,2,6) END,'YYYYMMDD')) >= CURRENT DATE - 7 DAYS
This will perform badly, so don't use that!
An alternative that will perform better is to convert CURRENT DATE - 7 DAYS into a number that matches your storage-format like this:
...WHERE ServiceDate >= '2'||substr(TO_CHAR(CURRENT_DATE - 7 DAY, 'YYYYMMDD'),2,6)
and if ServiceDate is INTEGER column datatype then:
...WHERE ServiceDate >= int('2'||substr(TO_CHAR(CURRENT_DATE - 7 DAY, 'YYYYMMDD'),2,6))
Always state your Db2-server platform (Z/OS, i-series, Linux/Unix/Windows) when asking for help with Db2, because the answer may be different depending on the platform + version of your Db2-server.
There are some similar questions on Stack Overflow (like questions 1923876, or question 266924 ) concerning SQL Server or TSQL. However I have received an ACCESS 2010 database and I'm trying to use the given solution with CAST etc. in ACCESS SQL, but with no success so far.
The dates are in 16 th century and there are two columns:
exact_date (datetime) and
estimated_year (int).
These columns should be used to sort using ORDER BY (assuming month and day as 1st January if only the year is known or estimated) the result.
Since CAST is not supported in Access SQL, consider DateSerial(year, month, day) to convert an integer year to a Date/Time value for Jan 1 of that year.
Here is an example copied from the Access Immediate window:
estimated_year = 1505
? DateSerial(estimated_year, 1, 1)
1/1/1505
? TypeName(DateSerial(estimated_year, 1, 1))
Date
Hello i have a table like this:
|user_name|pw|register_date|last_time_entered
I want to get all the rows where last_seen_date - register_date < 7
I dont know how to write this query i thought about something like this
SELECT * FROM workoutlog_1.personal
WHERE DATEDIFF(day, workoutlog_1.personal.register_date, workoutlog_1.personal.last_time_entered) < 7;
But i get this error:
Error Code: 1582. Incorrect parameter count in the call to native function 'DATEDIFF'
Thanks for helping.
Your error code seems to come from mysql.
With mysql, datediff takes only 2 parameters (day is not needed)
I think that in (really) old versions, it took 3 parameters, now it works only with 2, and it will return days.
If you had to work with another unit (hour for example), you could use TIMESTAMPDIFF
MySql's DATEDIFF differs from SqlServers DATEDIFF in that it takes only 2 date parameters, and returns the difference in days. Since you want days anyway, just remove the day parameter, i.e.
SELECT * FROM workoutlog_1.personal
WHERE DATEDIFF(workoutlog_1.personal.register_date,
workoutlog_1.personal.last_time_entered) < 7;
I'm getting the above SQL error after executing this query.
SELECT r.SectionIDNum, r.PeopleIDNum, r.Completed, c.CourseID, s.DistrictIDNum, s.EndDate
FROM Registration r, Course c, Section s
WHERE r.SectionIDNum=s.SectionID AND c.CourseID=s.CourseIDNum AND r.Completed='Y'
AND s.EndDate between ('2012-06-31', 'yyyy-mm-dd') and ('2013-07-01', 'yyyy-mm-dd')
Apparently, the commas in the dates are causing the error but I don't know how to fix it.
June only has 30 days in it. So SQL Server is confused by your request to cast June 31 as a date.
This works fine:
SELECT CAST('2012-06-30' AS DATE)
One way to avoid end of month issues is to use the DATEADD() function, for example, to get one year and one day prior to July 1, 2013 like in your example:
SELECT DATEADD(day,-1,(DATEADD(year,-1,CAST('2013-07-01' AS DATE))))
Also, remember that BETWEEN is inclusive, so you're getting June 30 and July 1 in your example, perhaps just subtracting the year is sufficient.
Use the CAST() Function (CAST converts value of one data type to a different type. In this case, CHAR to DATETIME):
SELECT r.SectionIDNum
,r.PeopleIDNum
,r.Completed
,c.CourseID
,s.DistrictIDNum
,s.EndDate
FROM Registration r
,Course c
,Section s
WHERE r.SectionIDNum=s.SectionID
AND c.CourseID=s.CourseIDNum
AND r.Completed='Y'
AND s.EndDate BETWEEN CAST('20120630' AS DATETIME)
AND CAST('20130701'AS DATETIME)
You could also use:
CONVERT(DATETIME,'20130701')
I have the following code in VBA to return the year between two dates: DateDiff("yyyy", "10/10/1930","06/07/2008 8:30:00 AM")
It returns 78, but it should really be 77.
What is going on here?
VBA's DateDiff function was not designed to track elapsed time. That statement is simply evaluating the year.
See this msdn article, which offers a function to calculate the years elapsed: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/workingwithelapsedtime.asp
Function elapsed_years_function(first_date As Date, Optional second_date As Date = 0) As Integer
' This procedure is from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/workingwithelapsedtime.asp
Dim elapsed_years As Integer
If second_date = 0 Then
' Did the caller pass in a date? If not, use
' the current date.
second_date = Date
End If
elapsed_years = DateDiff("yyyy", first_date, second_date)
If second_date < DateSerial(Year(second_date), Month(first_date), Day(first_date)) Then
elapsed_years = elapsed_years - 1
End If
elapsed_years_function = elapsed_years
End Function
Edit: According to this (VB.Net) As pointed out by #Justin, this is for VB.Net, not Visual Basic, but the implementation is most likely identical for backwards compatibility. I've referenced the relevant VBScript documentation below for completeness.
Larger Intervals. If Interval is set
to DateInterval.Year, the return value
is calculated purely from the year
parts of Date1 and Date2. Similarly,
the return value for
DateInterval.Month is calculated
purely from the year and month parts
of the arguments, and for
DateInterval.Quarter from the quarters
containing the two dates.
For example, when comparing December
31 to January 1 of the following year,
DateDiff returns 1 for
DateInterval.Year,
DateInterval.Quarter, or
DateInterval.Month, even though at
most only one day has elapsed.
Also see this (VBScript):
When comparing December 31 to January
1 of the immediately succeeding year,
DateDiff for Year ("yyyy") returns 1
even though only a day has elapsed.
So it's likely implemented like this, which gives 78:
Year(SecondDate) - Year(FirstDate)
See #Ken's solution for an implementation that should work as intended.
If you did DateDiff("yyyy", "12/31/2010", "1/1/2011") it would return 1, even though there is only a day difference.
Working as designed: see Remarks (larger intervals) under http://msdn.microsoft.com/en-us/library/b5xbyt6f%28v=vs.80%29.aspx WHICH STATES:
If Interval is set to DateInterval.Year, the return value is calculated purely from the year parts of Date1 and Date2.
When you do datediff by year the operation performed is 2008 - 1930