Calculate number of days from date represented as integer [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a column (EOM) in my table(Employee) which gives the last day of the month as an integer and I want to return the number of days in the month. I know we can normally use the built-in functions but since the DATE that I have is stored as an int they won't work. Can anyone help me out with this?
EmpID EmployeeName EOM
123 ABC 20160731
345 XYZ 20150228
I want to know the number of days that say Employee ABC worked for which is 31.

If you have the last day of the month in EOM column then you can simply take the last 2 characters from EOM column, which will give you number of days in the month.
SELECT SUBSTRING(EOM,7,2) FROM EMPLOYEE
OR
SELECT CONVERT(INT,SUBSTRING(EOM,7,2)) FROM EMPLOYEE
This will give you the number which you can work with to get the number of days Employee worked for.

Related

Max Date Minus X Days [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a date in the database.
I have to choose the maximum date and subtract x days from it.
it should happen according to the expression below I have selected a maximum date but I have to subtract certain days from date
DECLARE #MAX_DATE_VB as datetime
SET #MAX_DATE_VB = (SELECT MAX(CONVERT(DATE, Date_of_drop)) as MAX_DATE
FROM STORAGE
the desired result is to select the maximum date that is in the database but munis x days
I think you are looking for the dateadd function:
select dateadd(dd, -1, #max_date_vb) as new_date
You may change the second argument for the specific days you need to subtract.
See documentation for usage: https://learn.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-ver15

SUM HH.MM.SS IN SQL OR AS400 DATABASE [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to calculate total working hour from the table field as below:
table name : Attendance
field name : working_hours
select SUM(working_hours) from Attendance
In working_hours field i have data in format
empcode working_hours
123456 08.45.00
123456 10.06.00
123456 10.00.00
123456 09.06.00
I want this in Total hours like (17.50.00)
Thanks in advance.
the hour and minute sql functions will return the hour and minutes of the working_hours time field. You can sum up hours and minutes. Then divide total minutes by 60 to get the hours the minutes add up to. And the mod function returns the remainder of minutes divided by 60.
select sum(hour(a.working_hours)) +
sum(minute(a.working_hours)) / 60 ,
mod(minute(a.working_hours),60)
from attendance a

Access SQL Issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am curious if there are any experts in Access' version of SQL that could help me decode the below? I am not great in Access and try use SSMS but I am taking over an already built report.
Thanks!
LT CRD: IIf(Day(Date()+[IAM_MAN_LEAD_TIME]) Between 1 And 15,DateSerial(Year(Date()+[IAM_MAN_LEAD_TIME]),Month(Date()+[IAM_MAN_LEAD_TIME]),15),DateSerial(Year(Date()+[IAM_MAN_LEAD_TIME]),Month(Date()+[IAM_MAN_LEAD_TIME])+1,0))
In words, the code is saying
"If the current date + [IAM_MAN_LEAD_TIME] results in a date in the first 15 days of a month, then return the 15th of that month; else, return the date of the last day of the month."
For reference -
Date() returns the current date
Day() returns the day part of a date, e.g. Day(#2018-10-29#) = 29
DateSerial() returns a date given a year, month & day argument.
Year() returns the year part of a date, e.g. Year(#2018-10-29#) = 2018
Month() returns the month part of a date, e.g. Month(#2018-10-29#) = 10
Also note that DateSerial(Year, Month, 0) will return the last day in the previous month i.e. the day before DateSerial(Year, Month, 1)

SQL Server:sum rows by year and name the row value as 'current' for current year and 'previous' for last year [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to sql server and I want to sum a particular row for this year 2018 and for last year i.e. add data from jan 2018 to jun 2018 similarly for 2017 as well.
This is how I want the output:
Basically I want the year to date calculations for this problem, and the values column should be added for a particular year, I have given the months as it is my requirement to check if it falls within the month we are in, for example june. The data will be populated every month and thus I want the requirement to get it dynamically.
This will get you close:
SELECT row_number() over (order by year desc) as [S.No],
SUM([values]) as Total, year
FROM [table]
GROUP BY Year

Get Missing Month [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have result set like
DT_START DT_END
------------- -----------
2013-05-01 2014-04-01
2014-07-01 2015-04-01
2014-11-01 2015-02-01
i have start date and end date.i need missing month of 2014 in both dates.
Like missing month in 2014 is 5 and 6 (it is the difference between first row "DT_END" and second row "DT_START")
One possible solution:
Create a calendar table or make a query that returns all the months between the lowest dt_start and the highest dt_end. How you do this will depend on the actual database you use - some have nice functions that you can use to create sequences, some have number tables etc.
Then use that as source and do a left join with your table and filter out the rows where dt_start is null.
select month from all_months -- this is the table/query with all months
left join your_table on month between dt_start and dt_end
where dt_start is null