Comparing dates in Sql - sql

I am having trouble trying to compare dates in my sql query:
SELECT restrictions, restrictions_adddate FROM restrictions WHERE id = '555555'
AND restrictions_adddate BETWEEN ? and ?;
Both of the parameters system print in the format 'mm-dd-yyyy'. I am getting a wrong date format error, but have tried doing it in multiple ways. Any help is appreciated, and if more info is needed, please let me know. thanks.

This should work on any standard compliant DBMS:
SELECT restrictions, restrictions_adddate
FROM restrictions
WHERE id = '555555'
AND restrictions_adddate BETWEEN DATE '2011-01-01' and DATE '2011-12-31';

This is my preference:
AND restrictions_adddate BETWEEN
CONVERT(datetime, FLOOR(CONVERT(float, CAST('01-01-1900' AS DATETIME))))
AND CONVERT(datetime, FLOOR(CONVERT(float, Getdate())));
This allows you to compare dates (minus the time stamps).

Try checking out the CONVERT function T-SQL reference in BOL. You should be able to use this dictate the format of your dates.

Related

How can I switch the Month and Day when the column format is M/D/Y?

I am not sure if this has already been answered but I could not find anything.
I am trying to convert the column of dates from MM/DD/YYYY to DD/MM/YYYY.
I must confess that I don't even know where to begin on this. I was thinking I might try an update statement but I am unsure as to how to format it. Any help would be appreciated.
You need to do two level of conversation :
select convert(varchar(12), convert(date, datecol, 101), 103)
In general, you need to fix the data-type (if that is in your hand) instead of doing conversation in SQL. Date formatting should be done at the presentation layer.
You can try this by fixing the data type in the actual table. For displaying purpose either on a webpage, reports or whatever.
SQL Server provides a number of options you can use to format a date/time string. In select, you can try one of the suggested methods as here.
For all these conversions you need to pass the date values in proper data type which may be the date or date-time.
Here is one of the examples of your illustration.
declare #DateInString varchar(20) = Cast(getdate() as Varchar(20))
select convert(varchar(12), convert(date, #DateInString, 101), 103)
You could also do Select Format(datecol, 'dd/MM/yyyy')
This will return your datetime field as a varchar - you should then be able to insert that into your target.

How to get date from sql server in user specified format

I want get date from sql server in user specified format using GETDATE() function.
if i give this query
select GETDATE()
then it is displating output date in this format
2015-03-17 07:29:58.377
but i want output like this .
2015-03-17
what statement should be added with query to get the result.
help me from this problem.
Just use convert():
select convert(varchar(10), getdate(), 121)
Just have a look to the following link. which provides more conversion options.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
In SQL Server 2012 onward you may use FORMAT() which is a bit more intuitive than recalling style numbers.
e.g.
select FORMAT( GETDATE() , 'yyyy-MM-dd' )
see: https://msdn.microsoft.com/en-AU/library/hh213505.aspx
however do note the second parameter is case sensitive
'yyyy-MM-dd' works
'YYYY-MM-DD' would only work for the MM

Compare dates in sybase query

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

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

How to apply a particular format of date of birth in a SQL query?

Suppose I want a particular format of date of birth, like (mm-dd-yyyy).
How can I do that in SQL?
Although some people have listed the proper syntax for this in multiple RDBMSs (you really need to indicate which one you're using), I'd like to point out that formatting your data is typically something that should be done by the front end of your application. That's not to say that there's never a reason to do formatting in SQL, but usually it's best to just pass it as a date/time to the front end and let the front end handle how it will represent it to the user. Hopefully, you understand the difference between a date/time and a string.
Use CONVERT function if it is SQL SERVER
Select CONVERT(VARCHAR(10),Birthdate,110) from YourTable
Assuming your RDBMS is SQL Server, you can do the following:
SELECT CONVERT(VARCHAR(10), [DateOfBirth], 110)
There's more information about date formatting here.
SQL Server
SELECT convert(varchar, getdate(), 110)
SELECT convert(varchar, DateOfBirth, 110) FROM YourTable
How to format datetime & date in Sql Server 2005
If you're using Oracle:
select to_char(your_date_column,'MM-DD-YYYY') from your_date_table;