MySQL date function not accepting sub queries - mysql

I am trying to run a sub query within my TIMESTAMP function in MySQL v5.7.11.
SELECT TIMESTAMP(SELECT NOW() AS currentDate);
I get an error saying
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT NOW() AS currentDate)' at line 1
What am I doing wrong here?

You need to add extra pair of brackets inside your subquery.
SELECT TIMESTAMP((SELECT NOW() AS currentDate));

Related

SQL Convert 'mm/dd/yy' string to date format

I have table named templog and I have a date column named 'tdate' which stores a string in the 'mm/dd/yy' format. I tried to convert using the following syntax but I receive an error.
SELECT convert(datetime,tdate,110) from templog
SQL query: Documentation
SELECT convert(datetime,tdate,110) from templog LIMIT 0, 25
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'tdate,110) from templog LIMIT 0, 25' at line 1
Any ideas on what I'm doing wrong?
For MariaDB, you want str_to_date():
SELECT str_to_date(tdate, '%m/%d/%y')
FROM templog

I want current week data

I want current week data from mysql but problem is my field is varchar how i get the data
Error Number: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'YEARWEEK(CURDATE()) IS NULL' at line 3
SELECT `E`.`event_id`
FROM (`tbl_events` as E)
WHERE YEARWEEK(date_format(str_to_date(E.event_end,'%m-%d-%Y'),'%Y-%m-%d')),YEARWEEK(CURDATE()) IS NULL
You just have an extra ) before the 2nd yearweek() call, which should go at the end of the expression. Syntax wise.
However, I think you are overcomplicating this a bit:
SELECT `E`.`event_id`
FROM (`tbl_events` as E)
WHERE YEARWEEK(str_to_date(E.event_end,'%m-%d-%Y'))=yearweek(curdate())
On the left hand side of the expression I get the week of the year from the record. This should equal to the week of the year for the current week, which is on the right hand side of the expression.

Mysql error in select statement for date type column

SELECT DAY_ADD(DATE(scp.create_date),INTERVAL scp.subscription_period_days DAYS)
FROM `subscription` scp;
I am getting error as :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'DAYS) FROM subscription scp
what would be solution if I am adding days using some column values. should I use some other mysql function. Please rectify.
Try this one:
SELECT DAY_ADD(scp.create_date,INTERVAL scp.subscription_period_days DAY)
FROM `subscription` scp;
INTERVAL DAY instead of DAYS should work better.

MySQL SELECT with fields less and greater than values

I am trying to do a select query on MySQL with phpMyAdmin or PHP with PDO.
SELECT 'uid' FROM 'clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' AND 'lng'>='22.90243'
However, phpMyAdmin says:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' A' at line 1
What is wrong with it?
'' creates a string literal in MySQL, so your query is selecting the literal "uid" from the literal "clusters," which is invalid. Use backtics (or nothing)
SELECT Uid FROM clusters WHERE lat <= 47.21125 AND lat >= 39.21125
AND lng >= 22.90243

does the existence of an asterisk in a select exclude other columns?

This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this error?:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*' at line 1
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).