Access SQL Issue [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 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)

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

If my input is January and year is 2016 then it need to take input as 01-jan-16to 31-jan-16 [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 3 years ago.
Improve this question
I need to do that in my procedure. Customer mention January and the year. My procedure received only month and year, but I need to convert this month and year in to 01-month-year to 31-month-year.
You can use to_date function to convert string to date (first date of the month) and then use add_month to add 1 month and then -1 day to calculate last day of the month (Or use last_day function) as following:
Start_date := to_date(in_month|| '-' || in_year,'month-yyyy');
End_date := last_day(start_date);
-- or
End_date := add_months(start_date,1) - 1;
Cheers!!
Edit: This answer doesn't work for Oracle DB, only Java DB.
The below can be ignored:
According to Oracle's java-db Docs' Month Function & Year Function
you should be able to solve your issue by simply adding the following conditions to your Query:
WHERE
YEAR(your_date_column) = %customer_input_year_as_number%
AND
MONTH(your_date_column) = %customer_input_month_as_number%
It'd be easier to help you if you could show us what you've tried so far, this answer is purely based off of the oracle docs and should in theory work for your case.

Calculate number of days from date represented as integer [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 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.

Please translate the following SQL statement [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
date(dateadd('d',-weekday(currentdate,crMonday),currentdate))+1
The code gives you the first day of the week based on the current date.
weekday determines what day of the week it is based on the week starting on a Monday (crMonday). DateAdd then subtracts that number of days from the current date. By adding a day to the result you will get the first day of the week.
date(
dateadd('d',
-weekday(currentdate,crMonday),
currentdate)
)+1
EDIT: This is the result using today's date. Work from the inner functions to the outer functions.
currentdate = "Tuesday, Oct. 6, 2015"
-weekday(currentdate, crMonday) = -2
dateadd('d', -2, currentdate) = "Sunday, Oct. 4, 2015"
date("04-OCT-2015")+1 = "Monday, Oct. 5, 2015

TSQL find first and last day of fiscal month with variable dates [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My company fiscal calender has variable start and end dates for example,
Period 1 - 04-01-2013 to 04-26-2013
Period 2 - 04-27-2013 to 05-29-2013
...
I have a date dimension in my SQL Server 2008 DB which stores all the fiscal month, and years.
It's easy to figure out MTD, YTD with calender dates but i can't seem to figure out the same for fiscal dates.
I am trying to create some calculated members based on our sales data.
So how can one get the first day of Period 2 for example.
Assuming your date table has the required fields, your query might resemble this:
select yourfields
from yourtables
join date on date.date = SomeOtherTable.date
join (select min(date) mindate
from date
where fiscal_year = TheYearYouWant
and fiscal_period = ThePeriodYouWant) temp on date.date = mindate
Note that a period number without a fiscal year will give you results from previous fiscal years as well. You handle this either with a fiscal year field, or by giving your fiscal periods names like 'F2013/14 P1'. Personally, I prefer a separate field for fiscal year.
Edit starts here
The query above gives you results for one day only. If you want results for the current period, something like this will work.
select yourfields
from yourtables
join date on date.date = SomeOtherTable.date
join (select fiscal_year fy, fiscal_period fp
from date
where date = current_date) temp on date.fiscal_year = fy and date.fiscal_period = fp
Note that different db's have different ways of returning the current date, and you did not specify yours.