find the difference between two date values in Sybase database - sybase-ase

I have understand that there are some functions days() in sybase iq, but in sybase ase, I think we have only date part option to get the number of days between two days.
My requirement is that i want number days between two days without comparing month or year between two days.
Any help appreciated!

Use datediff function:
Description
Calculates the number of date parts between two specified dates or times.
Syntax
datediff(datepart, {date, date | time, time | bigtime, bigtime | datetime, datetime | bigdatetime, bigdatetime}])
Parameters
datepart
is a date part or abbreviation. For a list of the date parts and abbreviations recognized by Adaptive Server, see Transact-SQL Users Guide.
date expression1
is an expression of type datetime, smalldatetime, bigdatetime, bigtime, date, time, or a character string in a datetime format.
date expression2
is an expression of type datetime, smalldatetime, bigdatetime, bigtime, date, time, or a character string in a datetime format.
Example 6
Finds the number of days between two times:
declare #a time
declare #b time
select #a = "20:43:22"
select #b = "10:43:22"
select datediff(dd, #a, #b)
-----------
0

Related

compare current year timestamp against mysql stored timestamp

I want to return from database just the rows added "this year" (current year). Creation date for each row is stored as a timestamp in a decimal format.
One method that i imagine is to get the timestamp range for current year and then select just the rows that fit this range. I have google it but with no success.
Is there a way to compare the given year against that database stored timestamp?
The usual pattern is as you describe... comparing the bare column to a range.
We can use an expression to derive the start and end of the range. In this case, the beginning of the year and beginning of the next year.
EDIT
If datatype of creation_date column is DATE, DATETIME, or TIMESTAMP format, then, as an example:
WHERE t.creation_date >= DATE_FORMAT(NOW(),'%Y-01-01') + INTERVAL 0 MONTH
AND t.creation_date < DATE_FORMAT(NOW(),'%Y-01-01') + INTERVAL 12 MONTH
We can test the expressions with a SELECT statement
SELECT DATE_FORMAT(NOW(),'%Y-01-01') + INTERVAL 0 MONTH AS d1
, DATE_FORMAT(NOW(),'%Y-01-01') + INTERVAL 12 MONTH AS d2
returns
d1 d2
---------- ----------
2016-01-01 2017-01-01
Doing the comparison on the "bare" column is a pattern that will allow for MySQL to use a range scan operation on a suitable index, if one is available.
If we were to wrap the column in a function, that would force MySQL to evaluate the function for every row in the table. So I don't recommend this pattern. But as an example:
WHERE YEAR(t.creation_date) = YEAR(NOW())
EDIT
The edited question says created_date column is stored as a decimal datatype.
As long as the stored values are canonical (That is, the value of a later date will always compare as "greater than" the value of any earlier date) ...
then the same pattern applies... compare the bare column to the start and end of the range.
WHERE t.decimal_col >= beginning_of_this_year
AND t.decimal_col < beginning_of_next_year
Just need to provide decimal values (or expressions that return the decimal values) representing the beginning_of_this_year and beginning_of_next_year.
Absent a more precise definition of what values are stored, and what dates those values values represent, I can't give a more specific example.
You can use a str_to_date function
STR_TO_DATE('31/12/2016 8:06:26 AM', '%d/%m/%Y %r')
and for filter
where year( STR_TO_DATE('31/12/2016 8:06:26 AM', '%d/%m/%Y %r') ) = 2016

Converting Date to ISO Week of Year in BigQuery

This is the query I am running:
SELECT date, COUNT(fullvisitorid), device.deviceCategory
FROM TABLE_DATE_RANGE([xxx.ga_sessions_],TIMESTAMP('2014-05-01'),TIMESTAMP('2014-12-31')) GROUP BY date, device.deviceCategory
This returns date and total users for each day broken out by device. Is there a way to convert this date (individual day) into ISO week of year? Ex: if the day was 2014-01-01 it would be bucketed into "1" for week 1.
BigQuery has WEEK function - so
SELECT WEEK(CAST('2014-01-01' AS TIMESTAMP)),
WEEK(CAST('2014-12-31' AS TIMESTAMP))
returns 1 and 53 respectively.
There's two ways to do this. One is using EXTRACT:
SELECT EXTRACT(ISOWEEK FROM TIMESTAMP '2018-06-25')
The other is using FORMAT_TIMESTAMP:
FORMAT_DATE('%V', DATE '2018-06-25')
EXTRACT has the benefit that it works the same for date, datetime and timestamp fields. Downside is that it has very limited output options, but one of them is isoweek so it serves your purpose.
FORMAT_DATE comes with a family of functions (FORMAT_TIMESTAMP, FORMAT_DATETIME, FORMAT_TIME) but you have to use the right one depending on your data type. On the plus side, you can do very specific transformations using combinations of supported format elements.

Get week from unix timestamp retrieved from database

I have a MySQL database. In a couple of tables, the information that gets stored needs to be retrievable by week. So, I want to be able to do a SELECT FROM *database* WHERE week = *week*. The problem that I have is that the week part is stored as a unix timestamp (to allow for more versatilty like getting the date and time, just time, etc...).
So the question: How can I retrieve this record WHERE date = *date* when the stored date is a unix timestamp and date I'm matching it against is not?
If my question is too confusing and something needs to be rephrased or said in a clearer manner please comment and let me know.
MySQL has a built-in WEEK() method for handling dates: MySQL WEEK() Reference
Unfortunately however, MySQL's WEEK() method only supports DATE datatypes rather than a UNIX TIMESTAMP. Therefore, we must first convert the timestamp to a date so we can then pass that date to the WEEK() method:
SELECT
*
FROM
my_table
WHERE
WEEK(
DATE_FORMAT(
FROM_UNIXTIME('my_unix_timestamp_col'),
'%e %b %Y'
)
) = 51
If you have a column which is the DATE data-type, the query can be simplified (and can also use indexes):
SELECT * FROM my_table WHERE WEEK(my_date_col) = 51

Date search Sql Query

im storing the timestamp in mysql database in (INT) column, And i want to search the rows with between the dates. Anyone would please help what should be the Sql query to find the rows between two dates?
dates are entered like
FROM DATE = 15-10-2011
END DATE = 01-11-2011
It depends on what algorithm you use to convert the date strings to int values.
If the algoritm is mototonic, for example: If a day (say 15-10-2011) is converted to n (say 5037), then the next day (16-10-2011) is always converted to n+1 (so 5038 in this example.)
then you could just use:
WHERE IntField BETWEEN MySpecialConvertDateToIntFunction('15-10-2011')
AND MySpecialConvertDateToIntFunction('01-11-2011')
If your field stores different timsetamps as different integers (and the conversion is monotonic), you could change the above code slightly to:
WHERE IntField >= MySpecial...Function('15-10-2011')
AND IntField < MySpecial...Function('02-11-2011') --- notice the date+1
But it's usually better to use a field of the MySQL DATE type for storing dates. Unless you want to store dates before 1000 or after 9999 off course.
If you want to store timestamps, there's also a TIMESTAMP type. Read the
MySQL docs: DATETIME, DATE, and TIMESTAMP Types
You can use The BETWEEN operator, which selects a range of data between two values. The values can be numbers, text, or dates.
You can see there:
http://w3schools.com/sql/sql_between.asp
I would ask you to set data type as timestamp/datestamp & then
//php code
$date1=date ('Y-m-d' , strtotime ( "15-10-2011") );
$date2=date ('Y-m-d' , strtotime ( "01-11-2011") );
//sql code
SELECT * FROM tbl_mytbl WHERE DATE(attr_date) <'$date2' AND DATE(attr_date) >'$date1'
Can you use the mysql FROM_UNIXTIME function dev.mysql.com - function from_unixtime
SELECT *
FROM 'table'
WHERE FROM_UNIXTIME(intTimestamp)
BETWEEN
date ('Y-m-d' , strtotime ( '15-10-2011') )
AND ('Y-m-d' , strtotime ( '01-11-2011'));
I had made a mistake with the date input but have fixed.

Convert fields to timestamp for comparison

I have a Postres database and want to compare two timestamp fields.
Basic query:
select t1.valu1, t1.valu2 from table1 as t1 where t1.valu1 == t1.valu2
With timestamp comparison:
select t1.valu1, t1.valu2 from table1 as t1
where EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE t1.valu1 )
== EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE t1.valu2 )
Sample values for valu1:
'14:50:15', '10:50:15'
valu2:
'11:10:15', '17:50:15'
I want to convert valu1 and valu2 to timestamps and compare them afterwards.
// syntax of extract
EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '20:38:18');
I get an error in the following format:
EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE t1.valu2 )
Your sample values are text representations for time not timestamp:
'14:50:15', '10:50:15', '11:10:15', '17:50:15'
Assuming text as source type according to later comment.
It would make sense to cast them to time to eliminate insignificant white space or leading 0 from the comparison. But for a simple cast, it has to be a valid time format:
SELECT valu1, valu2
FROM table1
WHERE valu1::time = valu2::time
If you'd really want to compare "as timestamps", you'd need to use to_timestamp() with a matching format pattern:
SELECT valu1, valu2
FROM table1
WHERE to_timestamp(valu1, 'HH24:MI:SS')
= to_timestamp(valu2, 'HH24:MI:SS');
But this hardly makes any sense ...
You have to give, at least, a day, e.g.:
select EXTRACT(EPOCH FROM '2010-07-05 20:38:18'::timestamptz);
gives
date_part
------------
1278358698
You can just cast them:
select dateintext1::timestamp - dateintext2::timestamp from ...
Note that you can cast to time, date, timestamp, etc. As long as the format in the field is correct.
Far better to convert the text fields to date, time, or timestamp fields.
alter table abc alter column texttime type timestamp using (texttime::timestamp);

Resources