I'm trying to select only specific hours from my table that contain "Datetime" and other variables.
Example 1:
select distinct(Datetime) from NWP_OUT where HOUR(Datetime) = '00';
this works but when i try to select not only '00' no luck and no result
Example 2:
select distinct(Datetime) from NWP_OUT where HOUR(Datetime) = '00' and '06';
only show the first
I tried using 'between' but then i got 02,04,and 05
My end goal is to select only the data for hours: '00,03,06,09,12,18,21'
it is 10 years of data so manual selection will be hard.
Thank you all in advance!
Use IN:
SELECT DISTINCT(datetime)
FROM NWP_OUT
WHERE HOUR(datetime) IN (0, 3, 6, 9, 12, 18, 21)
You want to use IN
SELECT
DISTINCT(Datetime)
FROM
NWP_OUT
WHERE
HOUR(Datetime) IN ('00','03','06','09','12','18','21')
Related
I am trying to get value of 12 equal parts of the night length.
This is what my table looks like:
sunrise_time sunset_time Day_Length Night_length
2014-01-01 06:02:41.000 2014-01-01 20:44:05.000 14:41:24.0000000 09:18:36.0000000
This is my query, but getting day_length instead of night_light:
select (convert(varchar(10),dateadd(ss,abs(datediff(ss,sunrise_time,sunset_time))/12,0),8)) as nighthour
from table1
Expected output: 00:46:33
Actual output: 01:13:27
What's wrong with my query?
Switch your start date and end date around in the datediff to avoid issues. Here's a small change to your code.
select convert(varchar(10), dateadd(ss, datediff(ss, 0, night_length) / 12, 0), 8)
from table1
I want to know if a date (Year-Month-Day) is between a day of the month (Month-Day).
For example, I want to know if 'April 2, 2013' is between 'April 2' and 'April 19'.
I can easily do this of the range has a year value. Howver, without a year, I need ideas how to do this.
Some examples of what I'm trying to achieve:
SELECT 1 WHERE '2013-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-03-02' BETWEEN '04-01' AND '04-19'; -- result is 1
Thanks
You can make use of DATE_FORMAT().
Example
SELECT
*
FROM
table
WHERE
DATE_FORMAT(date,'%m-%d') BETWEEN '04-01' AND '04-19'
Edit
SELECT
*
FROM
table
WHERE
STR_TO_DATE(date, '%m-%d') BETWEEN '04-01' AND '04-19'
Following is work with oracle
select * from table_name where to_char(date_column, 'MM-DD') BETWEEN '08-01' AND '08-14'
The accepted answer is good, but it does not work if you want to select from December to February as #tony-brix said. You can extend that answer with two DATE_FORMAT functions. One goes from December to end of year, other goes from start of year to February.
SELECT
*
FROM
table
WHERE
DATE_FORMAT(date,'%m-%d') BETWEEN '12-01' AND '12-31'
OR
DATE_FORMAT(date,'%m-%d') BETWEEN '01-01' AND '02-01'
I have a huge table of 2.000.000 rows in which I have two columns that are dates. Now I want to find all those rows where the difference between the two dates is > 30.
I have already set a query of this sort:
SELECT DATEDIFF(date1, date2) as new
FROM table1;
and it works and list all the results and shows the difference. Now, I would like to have as a result only those cases where the difference between the two dates is higher than 30, and also have the evidence of this number.
I have tried
SELECT DATEDIFF(date1, date2) >30 as new
FROM table1;
But as a result in column new I have always value 1 (which I think stands for TRUE), but I need to see this difference (the number: 31, 32, 33 ecc.).
Can someone please help me?
thank you
If I'm not mistaken, try
SELECT datediff(date1, date2) AS new FROM table1 WHERE new > 30;
The > operator can be used as a boolean and is comparing your datediff() and your 30. That's why you should place it in a WHERE statement, to prevent a boolean value in your select.
select datediff(date1;date2) >30 as new from table1
gives values: 1 because, it returns whether datediff(date1;date2) > 30is true or not!
Try this
SELECT datediff(date1;date2) as new FROM table WHERE datediff(date1;date2) >30;
SELECT DATEDIFF(date1, date2) as new
FROM table1
WHERE DATEDIFF(date1, date2) > 30;
I have few tables
table201202
table201203
table201204
table201205
...
tableversion
In tableversion I have columns:
Version_ID Admin_ID MONTH YEAR
I need to get something like this
Select *
FROM tableversion
LEFT JOIN table(tableversion.month tableversion.year) on
table(tableversion.month tableversion.year).Version_ID=tableversion.version_id
But in tableversion I've got months without leading 0 so i must check if there is 5 and then change it to 05, if variable is 12 then nothing. How can I do it?
Ok, so now i have something like this
SELECT *, CONCAT('table',IF(tableversion.month < 10, CONCAT('0', tableversion.month ), tableversion.month ),year) as year
FROM tableversion
LEFT JOIN year ON tableversion.version_id=year.version_id WHERE ADMIN_ID=11
#1146 - Table 'database.year' doesn't exist
But it does not work
As seen here, use LPAD:
SELECT LPAD(month,2,'0') ...
Try this out in the area where you're concerned about comparing the months
IF(tableversion.month < 10, CONCAT('0', tableversion.month), tableversion.month)
I think an IF statement would likely perform poorly, but I have no data to back that claim.
You can try this alternative to an IF statement to achieve the same:
SELECT *, LPAD(month, 2, '0') as month2 ....
Dont know if i am breaking comuntiy guidelines by posting a continuation question as new question. If so. I am sorry!!
Now, using,
SELECT count(alertid) as cnt,date(alertdate) as alertDate
FROM alertmaster a,subscriptionmaster s
WHERE alertDate BETWEEN DATE_SUB(CURDATE(),INTERVAL 7 DAY) AND CURDATE()
GROUP BY date(alertDate),s.subId
ORDER BY a.alertDate DESC;
produces:
13, '2011-04-08'
13, '2011-04-08'
13, '2011-04-08'
14, '2011-04-07'
13, '2011-04-07'
Where I want is:
39, '2011-04-08'
27, '2011-04-07'
How to achieve this?
The reason you are getting more than one row per date is because you have GROUP BY date(alertDate),s.subId. Just change your GROUP BY to
GROUP BY date(alertDate)
If you don't actually want separate groups for each s.subId,date combination.
Also the code you posted is missing a JOIN condition. This is one reason why using the explicit (ANSI 92) JOIN syntax is preferred.