Compare dates in sybase query - sql

i'm trying to make a conditional search in sybase database as below.
WHERE ( dc.TABLE.DATE_ID = DateAdd(d,-1,GetDate()))
The format of dc.TABLE.DATE_ID is 'Feb 6 2014'.
The scope is to search yesterday's data.
Can someone help me?
Thank you in advance :)

This way converts both value of dates to date type without time
WHERE cast(dc.TABLE.DATE_ID as date) = DateAdd(dd,-1,cast(GetDate() as date))
You can use also datediff function, you don't have to convert data to date type
WHERE datediff(dd,GetDate(), dc.TABLE.DATE_ID )=-1
More infomration
datediff

Related

SQlite compare dates without timestamp

In SQLite, how to compare date without passing timestamp?
The date format is 2018-03-18 08:24:46.101655+00 and I want to compare against only date part as 2018-03-18.
I have tried as where mydate='2018-03-18' but that didn't return any records.
Similarly, tried Date(mydate)='2018-03-18' but that didn't help either.
How can I compare dates ignoring the timestamp part?
select * from mytable
where strftime('%Y-%m-%d', mydate) = '2018-03-18'
This is not one of the supported date formats.
To extract the date part from the string, use substr():
... WHERE substr(mydate, 1, 10) = '2018-03-18'
It might be a better idea to store dates in a correct format in the database to begin with.
It is looking that there is problem with date format.
Sqlite doesn't understand data like '+00' in date.
So date() and strftime() will not work here if data type is 'timestamp with time zone'.
Try by using like clause.
Try using strftime
SELECT strftime('%Y %m %d', 'columnName');
you can find it here strftime.php

SQL Server 2008 query check date on same day

I've written a query to check whether more than one record exists on the same day. Currently the excerpt from my query that performs the restriction looks like this :
GROUP BY
entry_date
HAVING
COUNT(entry_date) > 1
As the entry date column is defined as a datetime, does it check against the full datetime or just the date?
Thanks.
if entry_date is DATETIME ,your group by wont work as expected.you need to CAST it to DATE.Cast(Datetime)to date is sargable as well.
GROUP BY cast(entry_date as DATE)
having count(cast(entry_date as DATE)) > 1
Since you don't cast or convert it to anything else, naturally it uses all data available. So it would group data together with the exact same datetime. Why would you expect anything else?
It should be full datetime.
CONVERT(date, entry_date)
should separate out the date.

T-SQL Dates using Convert() function?

I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.

Filter by Dates in SQL

I have a column in my table for dates (DateTime) and I am trying to create a WHERE clause that says, WHERE dates BETWEEN 12-11-2012 and 12-13-2012
A sample value of dates column = 2012-05-24 00:38:40.260
I want to say WHERE dates BETWEEN MM-DD-YYYY and MM-DD-YYYY.
I tried doing
WHERE dates BETWEEN ((convert(nvarchar(10), dates,110) = '2012-12-12') AND (convert(nvarchar(10), dates,110) = '2012-12-12'))
but doesn't seem to work. "Incorrect syntax near ="
Please help
EDIT:
Thanks for various options and description guys. Got it working with #RichardTheKiwi's options.
If your dates column does not contain time information, you could get away with:
WHERE dates BETWEEN '20121211' and '20121213'
However, given your dates column is actually datetime, you want this
WHERE dates >= '20121211'
AND dates < '20121214' -- i.e. 00:00 of the next day
Another option for SQL Server 2008 onwards that retains SARGability (ability to use index for good performance) is:
WHERE CAST(dates as date) BETWEEN '20121211' and '20121213'
Note: always use ISO-8601 format YYYYMMDD with SQL Server for unambiguous date literals.
WHERE dates BETWEEN (convert(datetime, '2012-12-12',110) AND (convert(datetime, '2012-12-12',110))
Well you are trying to compare Date with Nvarchar which is wrong. Should be
Where dates between date1 And date2
-- both date1 & date2 should be date/datetime
If date1,date2 strings; server will convert them to date type before filtering.

how to convert datetime value to another date only format?

my datetime field looks like this:
2011-02-07 06:51:32.000 (yyyy-mm-dd)
User input is in this format:
02-07-2011 (only the date)
This should be converted to:
02-07-2011 00:00:00.000
i tried CONVERT:
CONVERT(VARCHAR(10),myDateTimeField,113)
but this only works with date values not with datetime.
please help!
I used the following function:
CONVERT(datetime,Cast(myDateInput AS Char (10)), 105)
thanks for all your answers! and my apoligizes for changing my question!
select convert(varchar, myDateTimeField, 105) ...
Source: http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/ and http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx
Crystal has a function called Date; it takes either YYYY, MM, DD or a datetime as arguments and returns a date.
So I would suggest you make a formula to change the date to a datetime and display it.
date( mytable.mydatetimevar )
You should then be able to format this date as you want with the normal date field formatting options.
An alternative method of doing it is using Crystal's DatePart function to pull out the month, year, day and then restructure as required.
Try this:
select DATEPART(mm, myDateTimeField) + '-' +
DATEPART(dd, myDateTimeField) + '-' +
DATEPART(year, myDateTimeField)
I don't think it's surprising that a DATETIME field carries time information ;)
In fact, you shouldn't alter the field itself, as you would lose the database engine's ability to use date/time related functions with this field.
(the format used is ISO 8601 which helps the engine to easily identify the single parts)
As others said, it's good practice to format the date when you are retrieving it, either in your SELECT statement or in your application. (for more tips regarding this we would indeed need the information what sql server and what appliccation language you are using).
try converting with Style ID - 110
CONVERT(VARCHAR(10),myDateTimeField,110)
Result will be: 02-07-2011