I'm new to sqlite. I have a table called "matchs" containing the upcoming games in a club championship. I would like to retrieve the first next upcoming event.
The first column is date_session formatted as "YYYY-MM-DD HH:MM:SS"
So I made query like this:
SELECT * FROM matchs WHERE date_session > datetime('now');
2017_01_09 18:30:00
2017_01_09 19:30:00
2017_01_10 18:20:00
2017_01_10 20:40:00
2017_01_12 18:20:00
2017_01_12 20:40:00
2017_01_16 18:30:00
I don't understand why the rows from January 9th are included in the results. To check that date time('now') is working I just outputted it:
SELECT datetime('now');
2017-01-10 09:30:55
If I do the query on a condition date set by hand, it works fine:
SELECT * FROM matchs WHERE date_session > '2017_01_10 09:30';
2017_01_10 18:20:00
2017_01_10 20:40:00
2017_01_12 18:20:00
2017_01_12 20:40:00
2017_01_16 18:30:00
Of course, as I wish to always have the next upcoming game returned, I can't afford setting this date condition manually.
Can anyone help? I'm using SQLite version 3.15. date_session is stored as TEXT.
- and _ are different characters. In any string comparison, _ > -, so you end up comparing only the year.
Related
I want to show date in string format as current month name(String) and year(e.g -> SEPTEMBER, 2019). How to write query for this in PostgreSQL?
I tried this query:
select
date(date_trunc('month', current_date));
but it gives me only current months starting date.
Try to_char() and add the year formatter to the string like this:
SELECT to_char(current_date, 'MONTH YYYY')
This will return:
SEPTEMBER 2019
Here's a sqlfiddle
If you want to format the output of a DATE value, use to_char()
select to_char(current_date, 'Month, yyyy');
we can do it like this also in Postgresql -
SELECT TO_CHAR(NOW() :: DATE, 'Month , yyyy');
output - September, 2019
Hello i am quite new to ruby on rails so please be easy on me. I have a rails app which consist of a table displaying record. I have a search bar which filters the result of the table by date. My search bar select tag looks like this
select_tag :date, options_for_select(#previous_summary_history_dates.uniq), prompt: "Search by date", id: "summary-history-date-filter", class: "form-control"
This is my #previous_summary_history_dates in the controller from where the select tag gets the value from
#previous_summary_history_dates = SummarySheet.where(is_history: nil).pluck(:record_inserted_at).map{|a| a.strftime("%m/%d/%Y - %l:%M %p")}.uniq
I have set the application.rb file to this
config.time_zone = 'Pacific Time (US & Canada)'
config.active_record.default_timezone = :local
I have a query for the search bar, which display the record in the table filtered by date selected from the select tag, which is something like this
#summary_history = SummarySheet.where(record_inserted_at: DateTime.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_beginning_of_minute..DateTime.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_end_of_minute)
This query works but it gives me the output in UTC format which doesnt match with my PCT format of the select tag and i get result as null. If i change my select tag time zone to UTC i get the desired output.
i tried .in_time_zone("Pacific Time (US & Canada)") in my filter query to convert the date in PCT but that didnt work
Can some one please suggest me how can i keep the select tage in PCT and fire the query and still get the dates filtered output? Thanks in advance
I solved it by changing the query to
#summary_history = SummarySheet.where(record_inserted_at: Time.zone.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_beginning_of_minute..Time.zone.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_end_of_minute)
I have a SELECT statement that I am trying to convert from Netezza SQL to Impala SQL. The output looks something like 140612, which is a date that is obtained by subtracting 7 from the current date and then pulling out the monday of that week.
I need to have this readable for Impala, then format it, then turn it into a string.
The query is :
TO_CHAR(next_day(DATE(a.date)-7, 'Monday'), 'YYMMDD') AS START_DATE
Assuming a.date is a timestamp, and T is the day of the week (1 = Sunday, 7 = Saturday; for your example above, Monday = 2, so T = 2) you should be able to use use
date_add(a.date, 7 - pmod(dayofweek(a.date) - T, 7));
in place of next_day in the above query. Check out the documentation on Impala's built-in date and time functions for more detail.
Attendence
(
Stu_id int
Att_Id int
Att_Date datetime
Att_Num numeric(15,5)
)
This table basically contains attendence records. I am trying to find the logic to enter the rows for missing dates from 1 Jan 2012 till today.
Assume there is a single attendence record for this period row (1,1,'2012-05-06',1.20000). Then I would like to insert rows for each day from 1 Jan 2012 till today except the existing date with the same values for all the fields except the date field which should be the actual date.
I am trying to bulk insert all the rows but don't know how would I adjust the date field
and check for the existing date.
Thanks.
Try a stored procedure which loops through the dates.
Or better yet, make a dates table and select from that. This will be much faster. Something like:
INSERT INTO Attendence (Stu_id, Att_Id, Att_Date, Att_Num)
SELECT a.Stu_id, a.Att_Id, d.TheDate, a.Att_Num
FROM Attendence a
INNER JOIN Dates d ON d.TheDate BETWEEN '2012-01-01' AND GETDATE()
AND d.TheDate <> a.Att_Date
I would use the DateDiff function to figure out the amount of days inbetween the date in the function and the date in question. Make a DateTime variable and give it the value of 1 Jan 2012. Then set up a while loop that loops the amount of days from the DateDiff function.
In psuedo, the while loop would look something like this.
#DaysTill = DateDiff(days, 1/1/2012, #SomeDate)
While #DaysTill>0
##Check = Select Count(Att_Date) From Attendance where AttDate = DateAdd(days, #DaysTill, Jan 1st 2012)
IF ##Check = 0 THEN Insert Into Attendance VALUES #Stu, #Id, DateAdd(days, #DaysTill, Jan 1st 2012), #numeric
#DaysTill = #DaysTill - 1
END
The main things are understanding DateAdd and DateDiff. After that everything becomes relatively simple.
I have a column in a table in which we are storing date in DATETIME format. (DD-MON-RRRR HH24:MI:SS) - Database Oracle 11g.
Data Type of a column is DATE, and storing date in 01-01-2012 01:00 PM (i.e. jan 1, 2012) format.
entity
#NotNull
#Column(name = "dateColumnName")
#Temporal(TemporalType.TIMESTAMP)
private Date sampleDate;
I am fetching all data by passing date
SAMPLE_QUERY = "select * from TableA tab where tab.dateWithTime = :sampleDate order by tab.dateWithTime ASC "
singleDate is "Tue Jan 24 00:00:00 IST 2012" , fasttime :
1327343400000
The problem is I am passing only date in the query, though Date through which records are being fetched is in DATE TIME format i.e 01-01-2012 01:00 PM.
How can i change my query so that it fetches all the records in ascending order of DateTime.
If you want to fetch all times for that day, then change your query to be more like
SELECT ... WHERE dateField >= :lowerParam AND dateField < :upperParam
Oracle has no DATE TIME datatype. The DATE datatype contains both a date and a time component, down to the second. TIMESTAMPS get a bit more complicated.
If your dateWithTime column is indeed a DATE datatype, the ORDER BY dateWithTime ASC clause should order your results in ascending order.
You may not be displaying the time component of your date. You can convert a date to a string in that format with TO_CHAR( dateWithTime, 'dd/mm/yyyy hh24/mm/ss' ) or whatever format you want.
Edit:
Oh, do you mean you want to find the cases where the date component of the DATE matches, but you don't care about the time component? That can be handled in the where clause with something like:
WHERE TRUNC( tab.dateWithTime ) = TRUNC( :sampledate )
TRUNC by default truncates a date to the beginning of the day.