Generate month and year from date and store automatically in table - sql-server-2005

Im working on sql server 2005, i have a column 'Date' of datatype datetime and want to extract month and year separately and store it in two separate columns in the same table as 'Date'. How do i do this and make it happen automatically everytime a new entry is entered where i dont have to manually input month and date.

You can use computed columns or trigger.
I recommend to use computed columns. It's very easy to implement and maintenance.
To get year and month use YEAR and MONTH functions
Computed Columns in SQL Server
DateTime functions in SQL Server

Related

SQL Querying a Database Using The Current Date and Date associated with an Attribute

I am trying to query a from scratch database I created. For each job posting in my database, I store the date of which it was posted in the 'date' datatype, which is in the format of "DD-MON-YY". I want to know which job postings haven't been filled after one month after its posted date.
So far I can query my database and find which jobs haven't been filled, due to a particular attribute being null in my table. But I don't know how to add the condition that it has been at least one month.
SELECT JOBID
FROM JOB
WHERE HIRED IS NULL;
For reference I have created this database in Oracle, and am using SQL developer.
I guess to rephrase, how would I make that condition, would I add one month to the date stored and the compare it with today's date?
Compare your date column to the current date with -1 months added to it:
SELECT JOBID
FROM JOB
WHERE HIRED IS NULL
AND date_column <= ADD_MONTHS(SYSDATE, -1);

Formatting Dates Across Several Tables

I'm currently performing a union across 4 different tables in which the dates are currently a combination of date and datetime fields.
If I use "Format" to change the dates, it'll turn the date column into an nvarchar data type which I do not want.
How can I then, format each of the dates in each of the tables for uniformity while maintaining the data type as date in the outputted view?
Below is the SQL script I used previously, which turned my dates into an nvarchar data type
FORMAT(Date,'MM/dd/yy') 'Date'
The final data type would be date
The correct query for my question is convert(date,yourfields) per George Menoutis

Specify a computed column in SQL Server 2012 as an average

Can I specify a computed column in SQL Server that averages another column?
So for example
CREATE TABLE dbo.Sales
(
TimeOfSale dateTime,
AmountOfSale money,
TotalSalesForMonth AS ?(Sum(AmountOfSale) over TimeofSale by month),
AverageForMonth AS ?(Sum(AmountOfSale) over TimeofSale by month / # sales per month)
);
I'm sure I could do this as a stored procedure and make another table, but I'm just curious as to whether it is possible to do so with computed columns.
Yes, you can do this, but you would have to write a UDF that calculates the aggregations and call the UDF in the Computed column definition.
Be advised that while this can be done, it may not be the best idea: https://blogs.msdn.microsoft.com/sqlcat/2011/11/28/a-computed-column-defined-with-a-user-defined-function-might-impact-query-performance/

How to retrieve rows from database by passing month in where condition

How to retrieve rows from a SQL Server database by passing month in where condition, as I have used datetime datatype for the date column.
Is there any function is SQL Server, where I can send month and will get the row accordingly?
I would create an indexed computed column based on the month in the date column. This way queries will scale as you'll have a specific column indexed for querying against.
Using Month(yourdatecolumn) = somemonth is going to table scan (as in read every value to determine the month) at query execution time so wouldn't be a good idea for large volumes of data.
See this article for a commentary on why using functions in your where clause is bad.
SELECT * FROM table
WHERE MONTH(datecolumn) = 1
this will return all records for January

Storing time values in a database and getting the difference between two time fields

I am working on an application the stores time values in a database e.g Expected time of Arrival and actual time of arrival. What is the best way of storing these fields in a database?
What SQL query can I use to obtain the time difference between the two fields?
If you're on SQL Server 2008, use the TIME datatype. Declare those two fields
ExpectedTimeOfArrival TIME(7),
ActualTimeOfArrival TIME(7)
Read up on how to manipulate the TIME datatype here.
Once you have those two fields in place, you could add a computed column to your table that calculates the difference between those two, e.g. in minutes:
ALTER TABLE dbo.YourTable
ADD TimeDifference AS DATEDIFF(MINUTE, ActualTimeOfArrival, ExpectedTimeOfArrival)
and then you could query o that new column TimeDifference as if it were a normal, regular table column.