sql how can i use automatic timing - sql

I am new to MSSQL and learning more every day, I have a website that uses a field called 'LoginTime' which is an INT and whenever a user logins that field increases +1 for that particular user . What I would like to do now is to make that field automatically start back at 0 on the 1st day of each month. That way I can get averages of how many times users login within a month at the end of each month. What should i be looking into in order to allow my sql server to do this ... triggers , stored procedures etc..

Instead of maintaining a counter on the table, add a new record for each successful login. Throw a query together that aggregates login attempts by user by month-year... example as follows:
DECLARE #LoginTable TABLE
(
UserName VARCHAR(50),
LoginTime DATETIME
)
INSERT INTO #LoginTable VALUES ('user1', '2014-01-01')
INSERT INTO #LoginTable VALUES ('user1', '2014-01-02')
INSERT INTO #LoginTable VALUES ('user1', '2014-01-08')
INSERT INTO #LoginTable VALUES ('user1', '2014-02-01')
INSERT INTO #LoginTable VALUES ('user1',' 2014-02-16')
INSERT INTO #LoginTable VALUES ('user2', '2014-01-07')
INSERT INTO #LoginTable VALUES ('user2', '2014-01-09')
INSERT INTO #LoginTable VALUES ('user2', '2014-02-24')
INSERT INTO #LoginTable VALUES ('user2',' 2014-02-26')
SELECT UserName, LEFT(CONVERT(VARCHAR, LoginTime, 120), 7) AS [Year-Month], COUNT(Logintime) AS Logins
FROM #LoginTable
GROUP BY UserName, LEFT(CONVERT(VARCHAR, LoginTime, 120), 7)

Step 1 - Drop the column from the table
Step 2 - If you don't have one already, add a table to store logins. It's fields should be the userId and the date and time of logging in.
This way, you can see how often people logged in during any date range.

Related

Can't run SQL query due to type conversion failure, key violation, and more

I get an error when trying to run this SQL query in MS Access:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, 21/09/2015);
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, 05/03/2016, 1);
This is the error:
enter image description here
The Learner table looks like this:
CREATE TABLE Learner
(
learnerPersonIDPKFK INT NOT NULL PRIMARY KEY,
registrationDate DATETIME,
recommendedByLearnerPersonIDFK INT NOT NULL,
CONSTRAINT fk_recommendedByLearnerPersonIDFK
FOREIGN KEY(recommendedByLearnerPersonIDFK)
REFERENCES Learner (learnerPersonIDPKFK)
);
Try wrapping your dates in single quotes:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '21/09/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '05/03/2016', 1);
Depending on your localization settings for the DATETIME format, you may need to use the MM/DD/YYYY format. Remember, your passing in a varchar and letting SQL try to "guess" how to implicitly convert it:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '09/21/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '03/05/2016', 1);
Use a simple tester to verify if SQL can "guess" your date formating:
SELECT CAST('09/21/2015' AS DATETIME)

What's wrong with this SQL INSERT statement?

I want to insert a certain value into a certain field of five different rows altogether. But whenever I run this query it doesn't get executed. What's wrong with it, and how can I fix it?
INSERT INTO `employee`(`password`) VALUES ('abc') WHERE `id` IN (1,2,3,4,5);
An INSERT can't have a WHERE clause. It looks like you meant to do an update:
UPDATE `employee`
SET `password` = 'abc'
WHERE `id` IN (1,2,3,4,5);
Or perhaps a multi-row insert:
INSERT INTO `employee` (`id`, `password`)
VALUES (1, 'abc'), (2, 'abc'), (3, 'abc'), (4, 'abc'), (5, 'abc');
Also, FYI, you really shouldn't be storing passwords as plain text, which it looks like you may be doing.

How not to store a value in a column?

I've got me this nice table.
create table Records (
Id uniqueidentifier primary key,
Occasion datetime not null,
Information varchar(999) default ' '
)
When I execute the first line line below, I get an error. When I execute the second, I see NULL in the cell. How should I insert stuff into the table so that the default white space kicks in?
insert into Records values (newid(), convert(varchar(21), getdate(), 121))
insert into Records values (newid(), convert(varchar(21), getdate(), 121), null)
The error message is the defaulty one but I'll put it in here anyway, in case someone asks.
Column name or number of supplied values does not match table definition.
By using the DEFAULT keyword:
INSERT INTO Records VALUES (newid(), CONVERT(varchar(21), getdate(), 121), DEFAULT)
Or, better, by specifying the exact columns with the INSERT statement
INSERT INTO Records (Id, Occasion)
VALUES (newid(), CONVERT(varchar(21), getdate(), 121))
Or:
INSERT INTO Records (Id, Occasion, Information)
VALUES (newid(), CONVERT(varchar(21), getdate(), 121), DEFAULT)
Why is it better?
Because you shouldn't rely on the order of columns in your table.
Because you shouldn't rely on the fact that the table will never have any new or removed columns (which is when your query will break again).
Also interesting: SQL Column definition : default value and not null redundant?
insert into Records (
Id
Occasion
)
values (newid(), convert(varchar(21), getdate()), 121))
Try this.
when you don't specify column names in insert query, sql-server expects values for all the columns. that is why your first query is throwing error as you have supplied value for two columns only, whereas your second query is running correctly
Try this:
insert into Records values (newid(), convert(varchar(21), getdate()), 121)
You have to close the ( of convert.

Missing Semicolon at the end of sql statement Access

I am trying to execute the following code. However, I continue to recieve the following 'Missing semicolon (;) at the end of SQL statement error in Microsoft Access.
The first query creates the table with the columns defined.
create table test
(
ProcessID int,
Name varchar(10),
Address varchar(10),
RandomData varchar(10)
);
The second query is causing the missing semicolon error.
INSERT into test
VALUES (123 , 'TestName', 'TestAdd', 'qwrj3ri'),
(456 , 'TestName2', 'TestAdd', 'qwerty'),
(789 , 'TestName', 'TestAdd', 'qwrj3ri'),
(1234, 'Testing123', 'tester', 'asdfghjk');
Code with amendments per above comments to make it Access friendly & remove typos:
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (123 , 'TestName', 'TestAdd', 'qwrj3ri');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (456 , 'TestName2', 'TestAdd', 'qwerty');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (789 , 'TestName', 'TestAdd', 'qwrj3ri');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (1234, 'Testing123', 'tester', 'asdfghjk');
Useful reference: https://msdn.microsoft.com/en-us/library/bb243852(v=office.12).aspx
Specific comments:
#Damien_The_Unbeliever:
I don't think access supports multiple rows in the values.
Amended to include an insert into per row instead of a value set per row (values (...), (...)).
#Thomas Tschernich:
our missing single quote next to the end of your insert
Changed 'tester', sdfg') to 'tester', 'sdfg');
#JohnLBevan:
superfluous character on end of first set of values
Changed 'qwrj3ri'), T to 'qwrj3ri'),
You can insert multiple rows in one insert statement in SQL server,but in MS ACCESS its not possible as above listed.
More techniques on multiple inserts in access are described
beautifully here

SSRS: How do I display a list of customers, from A - K then L to Z, without having two separate reports?

I would like one report with a drop down menu, showing:
Customers A-K,
Customers L-Z
All Customers
I have an SQL table for customers, with the standard columns (Acc No., name, address, etc).
I would like this split by name as it takes a long time to bring the result of the entire table down.
I would prefer this to be in one report. Is this possible? I am using SQL Server 2008 R2 with reporting services.
I have tried a few different methods with no luck, so I'm open to any suggestions!
Thanks for reading my question and thanks in advance if you can find the time to help!
Please feel free to ask any questions.
create table #alphab
( alphaname varchar(15));
insert into #alphab VALUES ('ANT')
insert into #alphab values ('CAT')
insert into #alphab values ('pAT')
insert into #alphab values ('mAT')
insert into #alphab values ('dAT')
insert into #alphab values ('rAT')
insert into #alphab values ('dAT')
insert into #alphab values ('lAT')
insert into #alphab values ('cAT')
insert into #alphab values ('zAT')
insert into #alphab values ('xAT')
insert into #alphab values ('wAT')
insert into #alphab values ('oAT')
insert into #alphab values ('sAT')
insert into #alphab values ('yAT')
insert into #alphab values ('uAT')
select alphaname
from #alphab
where alphaname LIKE
CASE
WHEN #alphabetorder = 1 THEN ('[A-K | a-k]%')
WHEN #alphabetorder= 2 THEN ('[L-Z | l-z]%')
END
order by alphaname