Incorrect syntax near ',' (within the dates not working) - sql-server-2008

I'm getting the above SQL error after executing this query.
SELECT r.SectionIDNum, r.PeopleIDNum, r.Completed, c.CourseID, s.DistrictIDNum, s.EndDate
FROM Registration r, Course c, Section s
WHERE r.SectionIDNum=s.SectionID AND c.CourseID=s.CourseIDNum AND r.Completed='Y'
AND s.EndDate between ('2012-06-31', 'yyyy-mm-dd') and ('2013-07-01', 'yyyy-mm-dd')
Apparently, the commas in the dates are causing the error but I don't know how to fix it.

June only has 30 days in it. So SQL Server is confused by your request to cast June 31 as a date.
This works fine:
SELECT CAST('2012-06-30' AS DATE)
One way to avoid end of month issues is to use the DATEADD() function, for example, to get one year and one day prior to July 1, 2013 like in your example:
SELECT DATEADD(day,-1,(DATEADD(year,-1,CAST('2013-07-01' AS DATE))))
Also, remember that BETWEEN is inclusive, so you're getting June 30 and July 1 in your example, perhaps just subtracting the year is sufficient.

Use the CAST() Function (CAST converts value of one data type to a different type. In this case, CHAR to DATETIME):
SELECT r.SectionIDNum
,r.PeopleIDNum
,r.Completed
,c.CourseID
,s.DistrictIDNum
,s.EndDate
FROM Registration r
,Course c
,Section s
WHERE r.SectionIDNum=s.SectionID
AND c.CourseID=s.CourseIDNum
AND r.Completed='Y'
AND s.EndDate BETWEEN CAST('20120630' AS DATETIME)
AND CAST('20130701'AS DATETIME)
You could also use:
CONVERT(DATETIME,'20130701')

Related

MySQL STR_TO_DATE Problem while using this function

As title, I'm trying to convert a VARCHAR column in a DATE column, and data is populated in that format "DDMMYYYY" ex. XMAS is "25122022" and in this case the correct formula should be STR_TO_DATE(column, '%d%m%Y')Well, when I execute this query I get an error since in some cases I have values with a "missing" char, I mean, for example, "1012023" when the day is <10 the query fails, cause it checks for "01122023" instead.I could solve this easily by adding a 0 to all fields having length 7, but I'd like to make it more clean.Reading better the usage of STR_TO_DATE I noticed that I could replace %d with %e since the second choice should theorically consider days from 0 to 31 instead of 01 to 31.Unexpectedly the query didn't work and gave me the same erorr at the first instance of a length 7 string.Am I doing something wrong?Thanks in advance.
We can try left padding your date string with zero to a length of 8:
WITH yourTable AS (
SELECT '1012023' AS dt
)
SELECT STR_TO_DATE(LPAD(dt, 8, '0'), '%d%m%Y') AS dt_out -- 2023-01-01
FROM yourTable;
Demo

Difference between two dates in MySQL with DATE() function

I am using DATE() function to calculate the difference between two dates in MySQL
value of SYSDATE() function is following
select SYSDATE();
2020-07-15 12:16:07.0
When I am using date from same month, it is giving correct result
select DATE(SYSDATE())- DATE('2020-07-13');
2
But when I am using date from last month it is giving difference as 86 instead of 16;
select DATE(SYSDATE())- DATE('2020-06-29');
86
Edit:
I am aware that we can use DATEDIFF() but I want to verify why DATE() function is giving results like this since we are already using this in code
MySQL doesn't support subtracting one date from another. The code
SELECT DATE '2020-07-15' - DATE '2020-06-29';
should hence result in an error, but MySQL silently converts this to this instead:
SELECT 20200715 - 20200629;
Seeing that you want to subtract two values, it assumes that you want to work with numbers. Dates are not numbers, but their internal representation yyyymmdd can be represented numerically. So, while CAST(DATE '2020-07-15 ' AS int) fails with a syntax error, as it should, MySQL is not consistent, when it comes to subtraction. It generates the numbers 20200715 and 20200629 and works with these.
I consider this a bug. MySQL should either raise an exception or return an INTERVAL when subtracting one DATE from another.

Selecting records between specific age range

I have a column (bday) which stores user's birthday info.
I searched similar questions and found that one
select * from users where datediff(year, bday, getdate()) between 18 and 22;
However, when I run this command I get
#1582 - Incorrect parameter count in the call to native function 'datediff'
What would be the correct way for this?
Error 1582 is a MySQL error, so presumably you are using MySQL.
The code you probably want is:
select u.*
from users u
where u.bdate >= curdate() - interval 22 year and
u.bdate < curdate() - interval 18 year;
This isn't exactly equivalent to the SQL Server (or Amazon Redshift) datediff(year . . . ) function. That would be more like:
select u.*
from users u
where year(u.bdate) - year(curdate()) between 18 and 22;
(Note: There might be an off-by-one error in this calculation.)
According to the documentation for MariaDB DATEDIFF only takes two arguments:
DATEDIFF(expr1,expr2)
expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation and it returns (expr1 – expr2) expressed as a value in days from one date to the other.
So your query should be:
select * from users where datediff(bday,getdate()) between 18 and 22;

MySQL - Date - Timestamp

My table has the below mentioned timestamp
Outcome required: data between 1997 and 1999 morning times i.e. (12:00:01 to 11:59:59)
1997-09-22 18:02:38
1997-10-15 01:26:11
1997-11-03 02:42:40
1997-10-15 01:25:19
1999-10-15 01:25:19
1999-10-15 23:25:19
1998-03-12 20:15:12
1998-02-13 23:52:53
1997-09-23 23:26:01
2000-09-23 23:26:01
I am trying the below query but does not give the right outcome
SELECT * FROM r WHERE ts BETWEEN '1997-01-01 00:00:01' AND '1999-12-31 11:59:59'
I can find the outcome by extracting hours and minutes separately but is there a way where the query is a bit concise?
You need to extract date and time separately to fetch the needed data.
In MySql you can use DATE_FORMAT method to extract same.
Read more here: DATE_FORMAT(date, format)
Your query will be:
SELECT * FROM `r` WHERE DATE_FORMAT(ts, "%Y-%m-%d") BETWEEN '1997-01-01' AND '1999-12-31' AND DATE_FORMAT(date_time, "%H:%i:%s") BETWEEN '00:00:01' AND '11:59:59'
If your date is not in DateTime format then you need to convert your string/raw date to date time format using STR_TO_DATE method.
Read more here: STR_TO_DATE(date, format)
You may use STR_TO_DATE function :
SELECT *
FROM r
WHERE ts >= STR_TO_DATE('1997-01-01', '%Y-%m-%d')
AND ts < STR_TO_DATE('2000-01-01', '%Y-%m-%d')
P.S: ts <= '1999-12-31 11:59:59' implicitly means ts < '2000-01-01'
There's no way to specify particular hours of day within the range comparison that spans years. We'd need to add another predicate (condition) to narrow down the rows that match the range scan.
We can use DATE_FORMAT function to get hours, minutes and seconds (formatted with two digits each)
For example, based on the stated specification (only times between 00:00:01 and 11:59:59) we could add something like this:
AND DATE_FORMAT(r.ts,'%h:%i:%s') BETWEEN '00:00:01' AND '11:59:59'
But it seems really strange to be omitting the second right after midnight, and the second immediately before noon. (MySQL DATETIME can have resolution smaller than a second, up to six decimal digits.)
Personally, I'd identify "morning hours" as simply hour values between 0 and 11, like this:
AND DATE_FORMAT(r.ts,'%h') BETWEEN '00' AND '11'
That will include "morning times" before 12:01 AM and after 11:59 AM. For example, these times would be included by this condition, but be omitted by the first example condition:
00:00:00.555
11:59:59.023
The specification isn't entirely clear... determining whether these times should be included or excluded would help clarify the specification. I suspect the statement of the specification is somewhat jarbled, and we really want all "morning times" between midnight and noon.
SELECT r.*
FROM r
WHERE r.ts >= '1997-01-01'
AND r.ts < '2000-01-01'
AND DATE_FORMAT(r.ts,'%h) BETWEEN '00' AND '11'
But it really depends on the definition of "morning hours", whether that first second after midnight is included or excluded.

SQL Error date is incompatible with int

Can Some One help me with this please. I was trying to use this Excel condition in SQL but I am getting error.
EXCEL Condition = "=DATE(2017,MONTH(EOMONTH(B2,0)+1),DAY(EOMONTH(B2,0)+1))"
SQL Query = DATEFROMPARTS(YEAR(GETDATE()),CONVERT(Varchar,
(MONTH(EOMONTH(hiredate,0)+1)),120),CONVERT(Varchar,
(DAY(EOMONTH(HireDate,0)+1)),120)) as 'DATE'
ERROR: Msg 206, Level 16, State 2, Line 1
Operand type clash: date is incompatible with int
Thank You in Advance
In Your Query Use DATEADD() Function OF SQL As Shown As Below,This May Work For You.
Use
CONVERT(Varchar,MONTH(DATEADD(dd,1,EOMONTH(GETDATE(),0))),120)
Instead Of
(MONTH(EOMONTH(hiredate,0)+1))
I guess you're trying to get the first day of the month after hiredate, but I can't tell for sure.
This MySQL expression generates that, using the handy-dandy LAST_DAY() function.
LAST_DAY(hiredate) + INTERVAL 1 DAY
If you want the first day of the month in which hiredate occurs, use this.
LAST_DAY(hiredate) + INTERVAL 1 DAY - INTERVAL 1 MONTH
If you're working with SQL Server (the Microsoft product) this expression gets you the first day of the month in which hiredate occurs.
DATEFROMPARTS(YEAR(hiredate), MONTH(hiredate), 1)
This gets you the first day of the next month.
DATEADD(MONTH, 1,DATEFROMPARTS(YEAR(hiredate), MONTH(hiredate), 1))
As you can see, it's different in SQL Server and MySQL. Job security for data base hackers. The built-in data handling functions deal correctly with stuff like 28, 29, 30, and 31-day months, and leap years, and all those calendar minutiae, so you don't have to.