Date and time Query - problem - sql-server-2008

I try to run this query:
select * from WorkTbl
where ((Tdate >= '20100414' AND Ttime >= '06:00') and (Tdate <= '20100415' AND Ttime <= '06:00'))
I have this date: 14/04/2010 and time: 14:00
I cant see hem, how to fix the query?
Thank's in advance

Have you tried formatting the date in the where clause like this:
'DD/MM/YYYY'

Your current query will only match rows where Ttime is exactly 06:00 (ie, this is the only time that matches both the >= '06:00' and the <= '06:00 criteria).
Try something like this instead:
SELECT *
FROM WorkTbl
WHERE (Tdate = '20100414' AND Ttime >= '06:00')
OR (Tdate = '20100415' AND Ttime <= '06:00')

SELECT *
FROM WorkTbl
WHERE (Tdate > '20100414' OR (Tdate == '20100414' AND Ttime >= '06:00'))
AND (Tdate < '20100415' OR (Tdate == '20100415' AND Ttime <= '06:00'))

Related

IF condition within WHERE

I need to know how to use an IF condition within a WHERE clause, I leave the idea in php:
SELECT * FROM table WHERE date >= '2017-08-04'
IF(date = '2017-08-04' ){
AND hour > '12:00'
}
so the idea is that if date equals today's date then add AND hour > '12:00'
Just use basic boolean logic. The exact translation (ignoring NULL values) looks like:
WHERE date >= '2017-08-04' AND
(date <> '2017-08-04' OR hour > '12:00')
A more sensible alternative:
WHERE date > '2017-08-04' OR
(date = '2017-08-04' AND hour > '12:00')
select * from table WHERE date >= '2017-08-04'
AND hour > '12:00'

What's wrong with my date range in this SQL statement?

I'm attempting to select data in a date range, but this is not working:
SELECT form_user.email
FROM form_instance JOIN
form_user
ON form_instance.form_user_id = form_user.form_user_id
WHERE form_instance.appointment_date >= '2017-04-13' AND <= '2017-05-11' AND
form_instance.status = 3
this is the issue
form_instance.appointment_date >= '2017-04-13' AND <= '2017-05-11'
this should be
form_instance.appointment_date >= '2017-04-13' AND form_instance.appointment_date <= '2017-05-11'
or use BETWEEN

How to search between dates ? mysql

I have this query :
SELECT * FROM tabl1
WHERE
(
created_time BETWEEN
DATE_FORMAT(NOW() ,'%Y-%m-02')
AND
DATE_FORMAT(NOW() ,'%Y-%m-02')+INTERVAL 1 MONTH
AND
DAY(NOW()) > 1
)
OR
(
created_time BETWEEN
DATE_FORMAT(NOW() ,'%Y-%m-02')
AND
DATE_FORMAT(NOW() ,'%Y-%m-02')-INTERVAL 1 MONTH
AND
DAY(NOW()) = 1
)
this query will return the columns that created last month,
but the month start at day number 2 and end at the second day of the next month !
so it will return :
3.3.2015
2.3.2015
but it will not return
1.3.2015
How to write the query with better way ?
Always do this:
where datefield >= YourStartDate
and datefield < TheDayAfterYourEndDate
Dateformat returns strings and is for display purposes only.
I think you can write the query like that:
SELECT *
FROM tabl1
WHERE DATE >= '2015-02-02 00:00' AND DATE_2 <= '20015-03-02 23:59'

How can I make a query for each month 7:15-12:15 in MySQL?

I found two similar answers
SELECT Count(*)
FROM fd_member
WHERE From_unixtime(Unix_timestamp(add_time), '%h') >= 8
AND From_unixtime(Unix_timestamp(add_time), '%h') < 12
GROUP BY CONVERT(add_time, CHAR(10))
SELECT *
FROM fd_member f
WHERE Date(f.date) >= Date('2013-10-01')
AND Date(f.date) < Date('2013-11-01')
AND Hour(f.date) >= Hour('8:00:00')
AND Hour(f.date) < Hour('12:00:00')
But they are not accurate to the minute.
SELECT *
FROM fd_member f
WHERE Date(f.date) >= Date('2013-10-01')
AND Date(f.date) < Date('2013-11-01')
AND Time(f.date) >= Time('7:15:00')
AND Time(f.date) < Time('12:15:00')
Should work. Give it a go. if not could you provide an SQL fiddle with some sample data and what you expect out

How can I get these 2 SQL queries to return 1 result set? (They select the same items, with different where clauses)

I want to merge this queries into only one, to return 2 values ("scount_atp" where estado=1 and "scount_xxx" where estado=4) by date:
SELECT DATE_FORMAT(FROM_UNIXTIME(fecha),"%m-%d-%Y") as sdate, COUNT(id) as scount_atp FROM entradas WHERE estado = '1' AND fecha <= UNIX_TIMESTAMP(NOW()) AND fecha >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 YEAR)) GROUP BY sdate
SELECT DATE_FORMAT(FROM_UNIXTIME(fecha),"%m-%d-%Y") as sdate, COUNT(id) as scount_xxx FROM entradas WHERE estado = '4' AND fecha <= UNIX_TIMESTAMP(NOW()) AND fecha >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 YEAR)) GROUP BY sdate
I'm a newbie at MySQL, so I'm sorry if this is too basic...
Thank you all for the help!
SELECT
DATE_FORMAT(FROM_UNIXTIME(fecha),"%m-%d-%Y") as sdate,
SUM(IF(estado = '1',1,0)) as scount_atp,
SUM(IF(estado = '4',1,0)) as scount_xxx
FROM entradas
WHERE estado IN('1','4')
AND fecha <= UNIX_TIMESTAMP(NOW())
AND fecha >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 YEAR))
GROUP BY sdate
Let me add an unrelated thing here: if the estado column is of type INT, you might want to write 1 instead of '1' - or you might up ending with a full table scan converting the int column to a string for comparison.
I did something similar in the past, this has worked for me:
SELECT
DATE_FORMAT(FROM_UNIXTIME(fecha),"%m-%d-%Y") as sdate,
COUNT(IF(estado = '1', 1, NULL)) as scount_atp,
COUNT(IF(estado = '4', 1, NULL)) as scount_xxx
FROM entradas
WHERE
fecha <= UNIX_TIMESTAMP(NOW())
AND
fecha >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 YEAR))
GROUP BY sdate