Count total days between current day (SQL) - sql

i'm trying to count the total days between current date and a specific column called "DayConfirm" (datetime). I want to show the total days in a new column beside the rows with "DayChanged" So far i got this:
SELECT DATEDIFF(CURDATE(),(DayConfirm, '%m/%d/%Y') AS DAYS
FROM Administr
can anyone help me? Thank you in advance.

You could try to make use of DATEDIFF:
SELECT DATEDIFF(DAY, GETDATE() , DayConfirm)

Your need to pass Date Part ie day here attribute:
SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate
also you missing the closing ); try this:
SELECT DATEDIFF(day, CURDATE(),(DayConfirm, '%m/%d/%Y')) AS DAYS
FROM Administr

Related

Finding the average time between two columns(VBA)

I wish to convert the dates from each column to minutes, sum all the minutes and find the average time between each row. I have to fetch the data from a SQL database and display the final amount of minutes in a textbox.
Thanks for any help!
-Remi
Edit:
I solved this problem like this:
SELECT Datediff(minute, startdate, enddate) FROM «table-name»»)
Answer:
I solved this problem like this: SELECT Datediff(minute, startdate, enddate) FROM «table-name»»)

Selecting the first day of the month in HIVE

I am using Hive (which is similar to SQL, but the syntax can be little different for the SQL users). I have looked at the other stackoverflow, but they seems to be in the SQL with different syntax.
I am trying to the get the first day of the month through this query. This one gives me today's day. For example, if today is 2015-04-30, then result would be 2015-04-01. Thanks!
select
cust_id,
FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') as first_day_of_month_transaction
--DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as first_day_of_month_transaction --SQL format. Not compatible in Hive.
from
customers;
Try this
date_format(current_date,'yyyy-MM-01')
To get the first day of the month, you can use:
date_add(<date>,
1 - day(<date>) )
Applied to your expression:
date_add(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'),
1 - day(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'))
)
But this will work for any column in the right format.
SELECT TRUNC(rpt.statement_date,'MM') will give you the first day of month.
You can use this query to calculate the First day of the current month
Select date_add(last_day(add_months(current_date, -1)),1);
Instead of current_date, you can use the date field name.
last_day => Gives the last day of current month
add_months with -1 => Gives previous month
date_add with 1 => Add one day
Another way - select concat(substring(current_date,1,7),'-01');

SQL Date Issue for Weekly Report Data

I need to run a weekly report based on the arrival date. How can I set the arrival date in the where clause so that I can get the result only for each week. The hard part is I DO NOT want to modify the dates each week. I need the permanent where clause for the date. I have to provide a list of customers who arrived every week and I just want to run the same script without changing the week dates.
Please assist.
Thanks.
SELECT * FROM TABLE WHERE
(ARRIVAL_DATE>DATEFROMPARTS(YEAR(GETDATE()-7), MONTH(GETDATE()-7), DAY(GETDATE()-7)))//7 days before starting at midnight
AND
(ARRIVAL_DATE<DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()))) //NOW in the YYYY, MM,DD format
This will get everything that happened in the current calendar week (Mon-Sun)
SELECT * FROM Table1
WHERE ArrivalDate BETWEEN
CAST(DATEADD(dd,(((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ,getdate()) as date) AND
CAST(DATEADD(dd,6+((((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ),getdate()) as date)
Edit - Removed extra E in BETWEEN

Subtracting two dates and taking months as output in MS SQL 2000

Please help to solve the issue.
I have Employee Table from that employee table details required are:
EmployeeName,no of months worked
no of months are calculated by resignation date - joining date which consists in the same table.
Thanks in advance.
Use DATEDIFF() function
DATEDIFF(datepart,startdate,enddate)
In query do this
Select EmployeeName,
DATEDIFF(mm,joiningDate,resignationDate) as monthsWorked
From table name
If you get all days and hours
declare #StartDate DATETIME = '2013-04-05 10:45:41.013',#EndDate DATETIME = '2013-04-07 14:45:41.013'
SELECT DATEDIFF(hh,#StartDate,#EndDate)/24 AS 'Days'
,DATEDIFF(hh,#StartDate,#EndDate) -
(DATEDIFF(hh,#StartDate,#EndDate)/24)*24 AS 'Hours'

T-SQL calculate with calendar week

I have a T-SQL question - I'm using SQL-Server 2012:
I have a varchar calendar week column (1-52). In my where clause I only want to select the last 6 weeks going back from the current calendar week of the current year. I also have a year varchar column with values of "2011" , "2012" and "2013".
But now I'm struggling to set the expression for the where clause.
Or do I need a Having clause to get this done?
I've tried it like this:
Having Calendarweek BETWEEN max(Calendarweek)-6 AND max(Calendarweek) AND Year=2013
but it returns all weeks.
Hope you can help me out with this.
thank you.
Try this
where Year ='2013'
and CAST(Calendarweek AS INT) >= datepart(week,getdate())-6