Subtract current Year from a SQL table - sql

How to subtract the current year from a table.
I have a table with a column VehicleYear. So I need to subtract only the current year from the VehicleYear to get the VehicleAge.
I tried the following query but it did not work since the SYSDATE seems to reflect the date when the row was added.
SELECT V. VEHICLENAME, (SYSDATE - V.VEHICLEYEAR) AS CURRENTAGE
FROM VEHICLE V

You can run this query to subtract year from two dates
SELECT VEHICLENAME,
EXTRACT(YEAR FROM sysdate) - EXTRACT(YEAR FROM VehicleYear) AS CURRENTAGE
FROM VEHICLE;
SQL Fiddle DEMO

Related

Bigquery SQL - Exclude rows/data for current Financial Year

I have a query in which I want to add a WHERE clause to exclude data for current Financial Year (without hardcoding it)
Note: FinancialYear 1st July to 30 June
SELECT * from TABLE A
WHERE FinancialYear != Current_Financial_Year
Based on a date column I have extracted Financial Year as below but not sure how to check if its current financial year and then exclude it using the WHERE clause above
extract(year from date_add(CalendarDate, interval 6 month)) as FinancialYear
You can compare to the current financial year by using similar logic on current_date:
where extract(year from date_add(CalendarDate, interval 6 month)) <>
extract(year from date_add(Current_Date, interval 6 month))
The best approach would be to use the following query
select * from Table_A where FinancialYear NOT LIKE '%2021%'
All you need to change is the year and you will get the respective data :)
Regards :)

SQL to display data of current month & next month from table

I wanted some guidance on producing an SQL query that collects the table information of the current date and also next month without having to type in every day for the current month being October or the next month being November.
Basically I've got a table called WORK, in this table there are SHIFTID, DATEOFSHIFT, and MEMBERSHIPID. I basically need to list the SHIFTID's of shifts where MEMBERSHIPID = null and where DATEOFSHIFT is in November (next month)
Then I need to produce a query for the shift roster showing SHIFTID, DATEOFSHIFT, and MEMBERSHIPID of each shift in this current month.
This is the structure of my database table if needed.
I would recommend:
select w.*
from work w
where w.membershipid is null and
w.dateofshift >= trunc(sysdate, 'Month') + interval '1' month and
w.dateofshift < trunc(sysdate, 'Month') + interval '2' month;
You can also phrase the where as:
where w.membershipid is null and
trunc(w.dateofshift, 'Month') >= trunc(sysdate, 'Month') + interval '1' month
but this makes it hard for Oracle to use an index if an appropriate one is available.
Well from what you've provided, I infer that you want a query to display the information on all those fields for the current month. That is achievable by:
Select SHIFTID, DATEOFSHIFT, MEMBERSHIPID
From WORK
Where Month(DATEOFSHIFT)=MONTH(GETDATE());

How to write a SQL query to retrieve all those customers whose age in months is more than 200 months?

How to write a SQL/Oracle query to retrieve all those customers whose age in months is more than 200 months?
I have a exam on Monday but I am having some confusion with months and dates calculation.
You can use a Query like this for MySQL:
SELECT *
FROM yourTable
WHERE bithdayField <= NOW() - INTERVAL 200 MONTH;
The logic is the same (the date is older than today minus 200 months), but the actual SQL is usually different, because DBMSes have a large variation of syntax in the date/time area.
Standard SQL & MySQL:
WHERE datecol < current_date - interval '200' month
Oracle:
WHERE datecol < add_months(current_date, -200)
In fact Oracle also supports the Standard SQL version, but it's not recommended, because you might get an invalid date error when you do something like '2018-03-31' - interval '1' month. This is based on a (dumb) Standard SQL rule which MySQL doesn't follow: one month before March 31 was February 31, oops, that date doesn't exists.
In Oracle DB, there are two nice functions : months_between and add_months
been used for these type date calculations. For your case, you may use one of the following :
select id, name, surname
from customers
where months_between(trunc(sysdate),DOB)>200;
or
select id, name, surname
from customers
where add_months(trunc(sysdate),-200)>DOB;
demo

Convert date into individual numerical columns for year month and day SQL

I have a date column in the format YY-MON-DD, e.g. 25-JUN-05. Is it possible to isolate this into 3 separate columns for year, month and day? Where month is converted from text to numerical, e.g. Year: 25, Month: 06, Day: 05?
MS SQL SERVER
As Nebi suggested, you can use DATEPART and extract each part and store it into different columns.
SELECT DATEPART(DAY,'2008-10-22'); -- Returns DAY part i.e 22
SELECT DATEPART(MONTH,'2008-10-22'); -- Returns MONTH part i.e 10
SELECT DATEPART(YEAR,'2008-10-22'); -- Returns YEAR part i.e 2008
Try with the below script,if you are using SQL Server.
SELECT 'Year: '+CAST(LEFT(YourdateColumn,2) as VARCHAR(2))+', Month: ' +CAST(MONTH('01-'+SUBSTRING(YourdateColumn,4,3)+'-15')as VARCHAR(2))+', Day:'+CAST(RIGHT(YourdateColumn,2)as VARCHAR(2))
FROM Yourtable
sample output :
You didn't specify your DBMS.
The following is standard SQL assuming that column really is a DATE column
select extract(year from the_column) as the_year,
extract(month from the_column) as the_month,
extract(day from the_column) as the_day
from the_table;

Trying to Look back 4 whole months in teradata with ADD_MONTHS function in sql statement

I'm trying to go back and retrieve counts for the last 4 full months. This is an example of what I have so far:
SELECT datecolumn, Count(datacolumnA) AS CountOfdatacolumnA, datacolumnB
FROM tableA
WHERE datacolumnB='AA' AND datecolumn >= ADD_MONTHS(CURRENT_DATE, -4)
My results show the last four months plus the current month, October in this case. The problem is that June isn't showing the correct count for the entire month. I'm only getting a partial count for the month.
You need to adjust to the start of the month. You can do this by subtracting the day of the month to get the '0th' of the month and then adding 1 to get the first. (I think dates in teradata are decimals with the int part being number of days since an epoch)
Select
datecolumn,
Count(datacolumnA) As CountOfdatacolumnA,
datacolumnB
From
tableA
Where
datacolumnB='AA' And
datecolumn >=
add_months(current_date, -4)
- extract(day from add_months(current_date, -4)) + 1