I used to get the Login Time of a user when he Logsin at the First Time to the Syatem, all the activities the user is doing will be stored under a seperate column called 'Activity'. My Problem is the User can LogOn to the System many times in a Particular Day, but i want to retrieve only the First Logon Time. The LogOn timings are appearing when i write a query for getting the Timings based on Activity are Like below.
Time User Activity
'3/23/2011 9:55:00AM' kk LogOn
'3/23/2011 5:30:00PM' kk LogOn
I write a Query to retrieve these values like this
Select StartDateTime,User,Activity from AgentLog where Activity='LogOn'. So, any one can give me Small help that how can i retrieve only the First LogOn time for the User given. I am Using Sql Server 2008, There will be Somany days, the User Logging On, so for every day i need to get the Logon Time
Get the earliest time for each day they logged in by using the following grouping:
Select min(StartDateTime)
from AgentLog
where Activity='LogOn' and
User = 'Username'
group by datepart(day, StartDateTime),
datepart(month, StartDateTime),
datepart(year, StartDateTime)
If you won't store times as a string but as a timestamp (in seconds) it would be easy to just select the data:
Select StartDateTime,Activity from AgentLog where Activity='LogOn' AND time>timeofday ORDER by time LIMIT 1
Where timeofday is the timestamp of the current day at midnight
Related
I want to query from snow flake db as part of monitoring process, How much time a user using snowflakedb to execute his queries after particular date. The purpose of this is, to prevent users to running long queries.
Account usage history is some thing I wanted to know. I'm very new to snowflakedb.
Is there any way to query from the metadata ?
You can use Query history view for this requirement
There are many columns in this view you can see and use appropriately as per your requirement.
Example Query :
SELECT query_id,
query_text,
query_type,
session_id,
user_name,
warehouse_name,
start_time,
end_time,
total_elapsed_time,
compilation_time,
execution_time
FROM snowflake.account_usage.query_history
WHERE user_name = 'anyusername'
AND Cast (start_time AS DATE) >= 'yourdate in yyyy-mm-dd format'
AND total_elapsed_time > 600000 -- around 10 minutes in milliseconds or you can specify any number here
AND warehouse_name = 'your datawarehouse name'
ORDER BY execution_time DESC;
There is also a parameter called STATEMENT_TIMEOUT_IN_SECONDS to control long running queries. Set to the amount of time, in seconds, after which a running SQL statement (query, DDL, DML, etc.) is canceled by the system. Can be set for Account » User » Session; can also be set for individual warehouses. The default setting is 172800 (2 days).
I'm trying to right an SQL Query that will do the following task:
Example:
ExpirationDate = '2018-04-30 00:00:00.000'
when the property is set to expire in 30 days ... 1 month before, I want SQL Server to email me informing that the item is about to expire based upon (Expirationdate)
could someone please give me some pointers as to where I go next... this is what I have tried so far..
the query below will find the item however I now want SQL Server Agent to send me an email for each row that is returned, not when nothing is returned
SELECT *
FROM [dbo].[Property]
WHERE expirationdate <= DateAdd(day ,30 , GetDate())
Here is an excellent link on how to email results from a sql server job agent:
https://www.brentozar.com/archive/2014/10/send-query-results-sql-server-agent-job/
As for your query: I wouldn't SELECT *. Explicitly select the columns you want returned in your email response.
Next I would define a variable to hold the value of your cutoff, this will prevent you doing a calculation for each row within the query. Example:
DECLARE #day30UpperBound DATETIME = DATEADD(Day, 31, cast(getdate() as date));
DECLARE #day30LowerBound DATETIME = DATEADD(Day, 30, cast(getdate() as date));
Then you can query results with:
SELECT explicitColumnList FROM [dbo].[Property]
WHERE expirationdate BETWEEN #day30LowerBound AND #day30UpperBound
This specific example should give you those Properties with an expiration date that fall in the window of 30 days from current at midnight, to 31 days from current at midnight - so a full 24 hour window 30 days from current day.
You can easily reproduce this for your 15 day, and 7 day reports. You could put all 3 reports into one job agent. Or you could make a different job for each of the 3.
You'll have to research a little on how to do only business days if that's what you want.
We have approximately 2 dozen SQL Database in our Azure portal. I have been asked to evaluate these and see if any are no longer being used and can be dropped. I know how to query their original Quotas and current sizes but I am baffled as to how to query DTUs.
How can I query each DB and see when someone last logged into or initiated any queries?
Thank you!
The following query should give you an idea if the database has been used based on resource consumption over the last 7 days:
SELECT *
FROM sys.dm_db_resource_stats
WHERE --database_name = 'AdventureWorksLT' AND
end_time > DATEADD(day, -7, GETDATE())
ORDER BY end_time DESC;
I'm writing the below query in SQL Server
update time_tracker
set logout = GETDATE(),
totaltime = SUBSTRING(CONVERT(varchar(20), (LOGOUT - LOGIN),120),12,8) from Time_Tracker
where userid = 0138039
and CONVERT(Date, LOGIN) = CONVERT(Date, GETDATE());
Basically, when the user hits a logout button I'm trying to achieve the below.
Enter the logout datetime stamp in my logout column
Calculate the difference between the login and logout time and update the total time column, bu checking for today's date and the current user.
When I do this, I'm getting this exception:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
Please let me know where I'm going wrong and how can I fix this.
Also when I run the below query
Select
SUBSTRING(CONVERT(varchar(20), (LOGOUT - LOGIN),120),12,8)
from
Time_Tracker;
It is working fine, I mean I'm getting the correct time output.
Thanks
You are trying to get hh:mm:ss from LOGOUT - LOGIN based on 12,8 in SUBSTRING function. And put them into float column.
I think it is better to store differences in integer datatype in milliseconds. Or use time datatype for that column.
Try to use DATEDIFF:
Select DATEDIFF(ms,LOGIN,LOGOUT) from Time_Tracker;
That will give you the time between two dates in milliseconds.
Or ALTER your column to time format.
So i'm doing this SQL SERVER school project and i want to add to a specific user the right to select for a limited amount of time lets say till 24 September 2016.
Is it possible and if yes how ?
THANKS!
There is no provision to grant access for a limited amount of time.
Best of luck.
The only way I can think of is creating a SQL Server Agent Job.
Create 2 steps in the job one for adding the user to database role db_datareader and then anotehr step to take away the role membership of that user.
The step to grant user permission should look something like
IF (GETDATE() >= '2016-09-24 08:58:59.000'
AND GETDATE() <= '2016-09-24 09:01:00.000')
BEGIN
EXEC sp_addrolemember N'db_datareader', N'UserA'
END
The second step to revoke user's permission should look something like
IF (GETDATE() >= '2016-09-24 16:58:59.000')
BEGIN
EXEC sp_droprolemember N'db_datareader', N'UserA'
END
Schedule the job to run twice a day. Occurrence every 8 hours. Assuming you only want the user to have access for 8 hours.
In the start date and end date options use 24 September 2016. So the Job is only executed on that date and never before or after that date.
Make sure that the interval between occurrences matches the logic in your IF statement of each step.
The job database context should be the database where your user need permissions.
I'd write a job that will run on that date that revokes the user's right.
I would NOT modify the schema to handle a one-off case. Control of rights to the database should be external to the database.
You can create a new column with a date datatype and call it endDate. You'd insert September 24, 2016 into endDate for a user and (depending on how you're accessing it) simply use an if statement comparing the current date and the end date. For example :-
if(CURDATE() < endDate)