Spec: I need to query by date month and day only (ignoring year)
Input:
start := '2015-12-29'
end := '2016-01-03'
Output:
1986-12-30
1980-01-01
1982-12-31
1978-01-03
...
Current solution:
SELECT *
FROM users
WHERE RIGHT(birthday,5) >= $1
AND RIGHT(birthday,5) <= $2
But this only works when the year not overlap, what's the easiest solution for this case?
My simplest solution is by generating the SQL string using another language:
if start_year == end_year {
date_where = `
AND RIGHT(birth_date,5) >= RIGHT(` + Z(start) + `,5)
AND RIGHT(birth_date,5) <= RIGHT(` + Z(end) + `,5)
`
} else {
date_where = `
AND ( RIGHT(birth_date,5) >= RIGHT(` + Z(start) + `,5)
AND RIGHT(birth_date,5) <= '12-31'
) OR ( RIGHT(birth_date,5) >= '01-01'
AND RIGHT(birth_date,5) <= RIGHT(` + Z(end) + `,5)
)`
}
I believe there are better way than this..
A date is not a string, don't use string functions to handle a date. Use date functions like EXTRACT:
SELECT '2015-12-29'::date,
EXTRACT(month from '2015-12-29'::date),
EXTRACT(day from '2015-12-29'::date);
Related
We INSERT new records every day to a table with say id and created_on column.
How do i identify if records with a particular identifier existed every day in the last 7 days ?
This can be done with a Stored Procedure:
CREATE OR REPLACE PROCEDURE TIME_TRAVEL(QUERY TEXT, DAYS FLOAT)
RETURNS VARIANT LANGUAGE JAVASCRIPT AS
$$
function run_query(query, offset) {
try {
var sqlText = query.replace('"at"', " AT(OFFSET => " + (offset + 0) + ") ");
return (snowflake.execute({sqlText: sqlText})).next();
}
catch(e) { return false }
}
var days, result = [];
for (days = 0; days < DAYS; days++)
if (run_query(QUERY, -days * 86400)) result.push(days);
return result;
$$;
CALL TIME_TRAVEL('SELECT * FROM TASK_HISTORY "at" WHERE QUERY_ID = ''019024ef-002e-8f71-0000-05e10030a782''', 7);
For the time travel query replace to work, put in an "at" as a table alias.
The return value is an array of day offsets when the query returns any value.
This will only work beyond DAYS=2 if you have Snowflake Enterprise Edition.
I did it with the below query
select id, sum(present) as total_count
from
(select id,feed_date, count(1) as present
from catalog_rca
where feed_date between '2019-11-19' and '2019-11-25'
group by 1,2) as temp
group by 1 having total_count = 7;
I am using bootstrap-datepicker and want to reset date month and year.
I found solution just for clearing the date:
$('#datepicker').val('').datepicker('update');
Do you have idea how can I clear month and year?
ANSWER:
I finaly found it:
var today = new Date(); //this is in format DD.MM.YYYY
var date = today.getDate() + '.' + (today.getMonth() + 1) + '.' + today.getFullYear();
$('#datepicker-wrapper').datepicker('update', date);
I want to compare dates in a TypoScript select.
Here's what I have (note that I commented the were clauses) :
lib.my_val = CONTENT
lib.my_val {
select {
pidInList = 100000
max = 1
#where = effective_date < CURDATE()
#where = TIMESTAMP(effective_date) < NOW()
orderBy = effective_date DESC
}
table = tx_my_table
renderObj = COA
renderObj {
5 = TEXT
5{
field = my_field
wrap = <span>|</span>
}
[...]
}
}
Which returns lines.
I tried to add a where statement any way I could with static dates or variables... without success. My understanding of the where clause is that everything after the = is dumped as is in the SQL query. But it seems I missed something.
Basically I want the TypoScript to generate a SQL Query smilar to this :
SELECT * FROM tx_my_table WHERE effective_date < NOW() ORDER BY effective_date DESC LIMIT 1;
This should be simple. Has anyone done this in the past?
Thanks!
Your TypoScript seems to be OK.
What happens if you enter the SQL Query directly into MySQL?
Note that with your code, only one record with pid=100000 is
selected.
Have you tried this:
--
lib.my_val {
select {
pidInList = 100000
max = 1
where = UNIX_TIMESTAMP(effective_date) < UNIX_TIMESTAMP()
orderBy = UNIX_TIMESTAMP(effective_date) DESC
}
table = tx_my_table
}
TYPO3 Wiki on select
What function can I use (similar to ZDATETIME() in ObjectScript) in Cache SQL to translate from a seconds from epoch timestamp to a UTC timestamp? I looked into the documentation and could only find information on doing this in Object Script... What function can you use in the SQL? Thanks!
I think you're looking for something like this;
DECLARE #epoch INT
SET #epoch = 1348519792
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, GETUTCDATE(), GETDATE()), DATEADD(s, #epoch, '19700101 00:00:00:000'))
SQL Fiddle
Figured it out, I had to convert it all manually. I spent forever trying to find a pre-existing function, but here is how I did it:
select
TO_CHAR(substr(audit.stamp,1,5),'YYYY-MM-DD') AS Date,
CASE LEN(CAST(substr(audit.stamp,7)/3600 AS INT))
WHEN 2 THEN CAST(substr(audit.stamp,7)/3600 AS INT)
ELSE '0' || CAST(substr(audit.stamp,7)/3600 AS INT)
END || ':' ||
CASE LEN(CAST((substr(audit.stamp,7)/60)#60 AS INT))
WHEN 2 THEN CAST((substr(audit.stamp,7)/60)#60 AS INT)
ELSE '0' || CAST((substr(audit.stamp,7)/60)#60 AS INT)
END || ':' ||
CASE LEN(CAST(substr(audit.stamp,7)#60 AS INT))
WHEN 2 THEN CAST(substr(audit.stamp,7)#60 AS INT)
ELSE '0' || CAST(substr(audit.stamp,7)#60 AS INT)
END AS Time
from utility.audit
As taken from the Cache SQL Reference Manual:
CLOCKTIME
NEW
SET Time=$PIECE($HOROLOG,",",2)
SET Sec=Time#60
SET Totmin=Time\60
SET Min=Totmin#60
SET Milhour=Totmin\60
IF Milhour=12 { SET Hour=12,Meridian=" pm" }
ELSEIF Milhour>12 { SET Hour=Milhour-12,Meridian=" pm" }
ELSE { SET Hour=Milhour,Meridian=" am" }
WRITE !,Hour,":",Min,":",Sec,Meridian
QUITCLOCKTIME
NEW
SET Time=$PIECE($HOROLOG,",",2)
SET Sec=Time#60
SET Totmin=Time\60
SET Min=Totmin#60
SET Milhour=Totmin\60
IF Milhour=12 { SET Hour=12,Meridian=" pm" }
ELSEIF Milhour>12 { SET Hour=Milhour-12,Meridian=" pm" }
ELSE { SET Hour=Milhour,Meridian=" am" }
WRITE !,Hour,":",Min,":",Sec,Meridian
QUIT
Thanks!
Is there a online system which converts SQL - LINQ or can anyone else help convert the SQL - LINQ below?
SELECT MIN(startTime) As startTime, MAX(endTime) As endTime
FROM tblRA
LEFT JOIN tblA ON tblRA.asID = tblA.asID
WHERE 'xxxxxx' BETWEEN tblRA.startDate AND tblRA.endDate
AND tblA.availabilityDayOfWeek = 7
The main area I am having trouble is the .MAX/.MIN.
Heres what I have so far
public List<string> GetResourceAvailabilitiesByDate(DateTime searchDate)
{
DayOfWeek dayOfWeek = searchDate.DayOfWeek;
var minVal = from a in dc.tblResourceAvailabilities
join b in dc.tblAvailabilities on a.asID equals b.asID
where searchDate.Date >= a.startDate.Date && searchDate.Date <= a.endDate.Value.Date
&& b.availabilityDayOfWeek == (int)dayOfWeek
select b.startTime.ToShortTimeString();;
var maxVal = from a in dc.tblResourceAvailabilities
join b in dc.tblAvailabilities on a.asID equals b.asID
where searchDate.Date >= a.startDate.Date && searchDate.Date <= a.endDate.Value.Date
&& b.availabilityDayOfWeek == (int)dayOfWeek
select b.endTime.ToShortTimeString();
var min = minVal.Min(minVal.Min);
var max = maxVal.Max();
return min,max;
Thanks in advance for any help
Clare
I think your code is a little bit incorrect, and the first symptom of it is that you are using repeated code to define minval and maxval. I tried to simulate something similar to what you want and came to the following code, please adapt it to your needs.
public List<string> GetResourceAvailabilitiesByDate(DateTime searchDate)
{
DayOfWeek dayOfWeek = searchDate.DayOfWeek;
var vals = from a in dc.tblResourceAvailabilities
join b in dc.tblAvailabilities on a.asID equals b.asID
where searchDate.Date >= a.startDate.Date && searchDate.Date <= a.endDate.Value.Date
&& b.availabilityDayOfWeek == (int)dayOfWeek
select b;
var min = vals.Min(v => v.startTime).ToShortTimeString();
var max = vals.Max(v => v.startTime).ToShortTimeString();
return new List<string>() { min, max };
}
Some comments on your code, assuming it's C#.
You are trying to return an array of strings when you should be returning an array of dates.
Your where clause is pretty confuse. You're comparing the search date with startdate.Date and endDate.Value.Date. It does not make much sense.
Your select clause could select only b, or a, or whatever. You don't really need to select the date in it.