I’m following along with a course so all of the queries they taught as well as notes, custom queries to practice and homework queries are in a single .sql file that I save and reopen as I follow along, I’ve come to the part with Timestamps and mathematical functions.
In the course he uses the command:
SELECT 0.1 * replacement_cost AS deposit
FROM film;
This is where I started encountering problems, this query didn’t work for me so i started googling how to fix it, after not finding the solution I tried the same query from the video again.
Again it did not work and just gave me a syntax error in a previous query a bit up, also when I just type easy queries like ‘SELECT * FROM film’ I keep getting syntax errors in previous lines, the queries that I write do work when I highlight them with my mouse.
This is also happening in a new .sql file, and now it keeps jumping up saying there’s a syntax error in line 27 when I’m trying to write in line 50+
The query the syntax error is occurring(line 27) is ‘SELECT AGE (payment_date) FROM payment;’
However when I highlight it it actually runs the query and shows the expected results
Here is the full text and notes from my second .sql file
--Timestamps and extract Part One--
--TIME - Contains only time--
--DATE - Contains only date--
--TIMESTAMP - Contains date and time--
--TIMESTAMPTZ - Contains date, time and timezone--
SHOW All
--SHOW TIMEZONE--
--SELECT NOW()--
--SELECT TIMEOFDAY()--
--SELECT CURRENT_TIME--
--SELECT CURRENT_DATE--
--Timestamps and extract Part Two--
--EXTRACT()
--YEAR--
--MONTH--
--DAY--
--WEEK--
--QUARTER--
--EXTRACT(YEAR FROM "date_col")Extract or obtain a sub-component of a date value--
--AGE("date_col")Calculates and returns the current age given a timestamp--
--TO_CHAR()General function to convert date types to text/Timestamp formatting--
--EXAMPLE: TO_CHAR("date_col", 'mm-dd-yyyy') Or other date format--
--This query can work with YEAR,DAY,QUARTER,WEEK,MONTH--
SELECT AGE(payment_date) >>>>>LINE 27 WHERE THE SYNTAX ERROR OCCURS<<<<<
FROM payment;
--You can format freely, ordering D/M/Y, use - or /--
SELECT TO_CHAR(payment_date,'DD-MM-YY') AS Paydate
FROM payment;
--Timestamps and extracts Challenge Tasks--
SELECT DISTINCT TO_CHAR (payment_date, 'Month') AS Paymonth
FROM payment;
SELECT COUNT (TO_CHAR (payment_date, 'dy')) AS days
FROM payment
WHERE TO_CHAR(payment_date,'dy') = 'mon'; --My solution--
SELECT COUNT(*)
FROM payment;
WHERE EXTRACT(dow FROM payment_date)=1; --Course solution--
--Mathematical functions and Operators--
SELECT ROUND(rental_rate/replacement_cost,4)*100 AS percent_cost
FROM film;
It’s mostly notes and queries, but they all end with ; and it just keeps jumping back up with an error syntax in line 27
In my big .sql file where the entire course is written in I’ve also encountered the ‘current transaction is aborted, commands ignored until end of transaction block’ error, which is what prompted me to start in a new file.
How do I prevent this from happening?
Thanks!
Stefan Wolters is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.