SQL compare DATE
Hello,
I know this isn't a SQL-forum but please help me:
I want to select by a SQLStatement an object whose 'date' is like the
'date' of today:
String sqlStatement = "SELECT * FROM OBJECT WHERE DATE = " + todayDate;
My question is:
Which format shoud have 'todayDate'? I tried 2003-07-30 but it doesn't work. Then I tried '2003-07-30 00:00:00'. This does't work, too!
PLEASE HELP!
Probably not possible without knowing which database you are using.
It would also help to know what you mean when you say it doesn't work. Does that mean it throws a SQL exception or that it doesn't return the results that you want.
Of course you do realize that the field you are comparing to must be an exact match or this won't work. If the field also contains time values then you have to use a range comparison.
Similar Messages
-
SQL: comparing date with datetime field
I'm having a brain fart as I assume this is really simple.
Given that I have the date in the format of yyyy/mm/dd, how
do I put that
into a sql query that compares that to the datetime field?
It appears the datetime field, by default, uses this format:
m/d/yyyy
hh:mm:ss am
-DarrelIf your date formats are different you will have to use CAST
or CONVERT
functions as you state you already have. If this works then
stick with it.
You may also need to use a SET DATEFORMAT command. See below:
http://msdn2.microsoft.com/en-us/library/ms189491.aspx
Dates in SQL Server can be a bit of a pain to work with. It
is usually best
practice to store your DateTime data in raw format and use
CAST or CONVERT
functions in your Stored Procs to get the correct results.
Pat.
"darrel" <[email protected]> wrote in message
news:ftloim$jv4$[email protected]..
>
>> What dbms?
>
> t-sql (MSSQL)
>
> For now, via string functions, I rearrange the date/time
format manually,
> and then use the CAST statement in the SQL query. Not
pretty, but seems to
> work.
>
> -Darrel
> -
yup.. how can i compare date in SQL statement??
pls give me a completed example.I'd think this is a formatting problem. Why not try:
PreparedStatement ps = myConnection.prepareStatement(
"SELECT * FROM Receipt WHERE to_date(Date) > ? ");
ps.setDate(1,TodayDate);
ResultSet rs = ps.executeQuery();
HTH,
Ken -
SQL Server Data Tools : Schema Compare
I have my SQL 2012 installed and also its' tools like management studio, visio studio shell 2010
I know there is a function inside visio studio shell call "schema compare"
how to enable/install this function ? extra license is required ?You can download SSDT(SQL Server Data Tools), install it. Then you can use it without extra license:
Download SQL Server Data Tools
more info plus tutorials:
Microsoft SQL Server Data Tools
T-SQL Articles
T-SQL e-book by TechNet Wiki Community
T-SQL blog -
SQL Server Data Compare doesn't retrieve table list
I'm trying to do a data compare in Visual Studio 2013. I have chosen my source and targets and successfully tested the connection to each of them in the connections dialog.
When I choose next, there's a few seconds delay as though the compare tool is checking with the source and target and then it shows a dialog with what to compare. There is a checkbox next to Tables and next to Views. Those are the only two options
shown.
What should be in this dialog, left of Tables and Views, is an arrow to expand them and show the list of tables and views in the source database. The arrow is missing and I cannot expand any list of tables or views. There's just nothing shown
to compare.
When I click Next again, the compare appears to run but when complete it reports that zero tables or views were compared - under the circumstances this is as expected because the previous dialog did not allow me to check any tables to compare.
Why am I not getting the arrow to expand the list of tables? What's wrong with Data Compare?
Thanks,
DaleHi Dale,
>>In your image you show a primary key value in comparison key but my tables did not have a primary key assigned
>>Is the comparison key requirement by design?
I repro it, I get the same issue as yours, if no key, we couldn't compare the tables.
Like the document here:
https://msdn.microsoft.com/en-us/library/dn266029%28v=vs.103%29.aspx?f=255&MSPPError=-2147217396
https://msdn.microsoft.com/en-us/library/aa833428(v=vs.100).aspx
Requirements
When you compare data in a table or view, the table or view in the source database must share several attributes with a table or view in the target database. Tables and views that do not meet the following criteria are not compared and do not appear
on the second page of the New Data Comparison wizard:
Tables must have matching column names that have compatible data types.
Names of tables, views, and owners are case-sensitive.
Tables must have the same primary key, unique index, or unique constraint.
Views must have the same unique, clustered index.
You can compare a table with a view only if they have the same name.
Each object has a key or an index that determines the other objects to which it corresponds. However, each table or view can have more than one primary key, unique index, or unique constraint. Therefore, you might want to specify which key, index,
or constraint to use.
As my understanding, it would have the specific requirements.
Best Regards,
Jack
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey. -
Hello, I am trying to compare data from one and the same table. In short I have:
NAME DTM CHARGE
Atanas 8/18/2012 9:33:23 AM 100
Atanas 8/18/2012 9:33:23 AM 101
Niki 8/18/2012 9:33:43 AM 200
Niki 8/17/2012 9:34:10 AM 100
Niki 8/18/2012 9:33:43 AM 201
Niki 8/17/2012 9:34:10 AM 101
Atanas 8/17/2012 9:34:29 AM 50
Atanas 8/17/2012 9:34:29 AM 51
Joro 8/18/2012 12:10:12 PM 400
Joro 8/17/2012 12:10:21 PM 300
Joro 8/18/2012 12:10:12 PM 401
Joro 8/17/2012 12:10:21 PM 301
And I want to sum CHARGE for each DTM for each NAME and present the difference in an additional column, the output should be like that:
NAME DTM CHARGE DTM CHARGE DIFFERENCE
Atanas 8/17/2012 9:33:23 AM 101 8/18/2012 9:33:23 AM 201 - 100
Niki 8/17/2012 9:33:23 AM 201 8/18/2012 9:33:23 AM 401 - 200
Joro 8/17/2012 9:33:23 AM 601 8/18/2012 9:33:23 AM 801 200
the DTM will be always /sysdate - 2/ and /sysdate - 1/,
Thanks in advance!It is not clear what you are trying to do, but it looks like:
with sample_table as (
select 'Atanas' name,to_date('8/18/2012 9:33:23 AM','mm/dd/yyyy hh:mi:ss am') dtm,100 charge from dual union all
select 'Atanas',to_date('8/18/2012 9:33:23 AM','mm/dd/yyyy hh:mi:ss am'),101 from dual union all
select 'Niki',to_date('8/18/2012 9:33:43 AM','mm/dd/yyyy hh:mi:ss am'),200 from dual union all
select 'Niki',to_date('8/17/2012 9:34:10 AM','mm/dd/yyyy hh:mi:ss am'),100 from dual union all
select 'Niki',to_date('8/18/2012 9:33:43 AM','mm/dd/yyyy hh:mi:ss am'),201 from dual union all
select 'Niki',to_date('8/17/2012 9:34:10 AM','mm/dd/yyyy hh:mi:ss am'),101 from dual union all
select 'Atanas',to_date('8/17/2012 9:34:29 AM','mm/dd/yyyy hh:mi:ss am'),50 from dual union all
select 'Atanas',to_date('8/17/2012 9:34:29 AM','mm/dd/yyyy hh:mi:ss am'),51 from dual union all
select 'Joro',to_date('8/18/2012 12:10:12 PM','mm/dd/yyyy hh:mi:ss am'),400 from dual union all
select 'Joro',to_date('8/17/2012 12:10:21 PM','mm/dd/yyyy hh:mi:ss am'),300 from dual union all
select 'Joro',to_date('8/18/2012 12:10:12 PM','mm/dd/yyyy hh:mi:ss am'),401 from dual union all
select 'Joro',to_date('8/17/2012 12:10:21 PM','mm/dd/yyyy hh:mi:ss am'),301 from dual
select nvl(s1.name,s2.name) name,
s1.dtm,
s1.charge,
s2.dtm,
s2.charge,
s2.charge - s1.charge diff
from (
select name,
dtm,
sum(charge) charge
from sample_table
where dtm >= trunc(sysdate) - 1
and dtm < trunc(sysdate)
group by name,
dtm
) s1
full join
select name,
dtm,
sum(charge) charge
from sample_table
where dtm >= trunc(sysdate)
and dtm < trunc(sysdate) + 1
group by name,
dtm
) s2
on s1.name = s2.name
NAME DTM CHARGE DTM CHARGE DIFF
Atanas 17-AUG-12 101 18-AUG-12 201 100
Niki 17-AUG-12 201 18-AUG-12 401 200
Joro 17-AUG-12 601 18-AUG-12 801 200
SQL> SY. -
Comparing data from two tables
My PL/SQL is very limited, however, I need to write some code that compares data stored in two different Oracle tables and then writes the differences to a third table. Both tables have primary keys that I will use to ensure I am comparing like for like. These tables could be large.
Any help on the best way to do this, or any code examples would be very appreciated.One fairly efficient way of doing this is with set operators. The following code is untested - but looks right ;). It also assumes you want to know the provenance of the rows; if not, just zap the literal from each SELECT statement.
INSERT INTO diff_tab SELECT * FROM
( (SELECT 'tab1nottab2' AS provenance, t1.col1, t1.col2 FROM tab1 t1
MINUS
SELECT 'tab1nottab2', t2.col_a, t2.col_b FROM tab2 t2)
UNION ALL
(SELECT 'tab2nottab1', t2.col_a, t2.col_b FROM tab2 t2
MINUS
SELECT 'tab2nottab1', t1.col1, t1.col2 FROM tab1 t1)
/Cheers, APC -
How to compare date which is greatest date
iam new to sql
how to compare date which date is greatest date with example
can i use to_char to compare date
SELECT eno, ename, GREATEST (TBLE.MAX1, TBLE.MAX2) recent_date "
FROM (SELECT TBLA.eno, TBLA.ename, "
MAX (TO_CHAR (TBL.date1, 'MM/DD/YYYY HH24:MI') "
) MAX1, "
MAX (TO_CHAR (TBL.date2, 'MM/DD/YYYY HH24:MI') "
) MAX2 "
FROM dual TBL, dual2 TBLA
in above query if any one of the date having null value then recent_date is showing 'null'
and also it showing wrong value
example
date1='08-22-2009' and date2='01-23-2010' then it showing wrong recent_datel ike='08-22-2009'
as according my knowledge it is comparing with month it sholud not be like this wat i have to do pls explain with example my o/p should come for recent_date like =''01-23-2010''
Edited by: user9112274 on Aug 27, 2010 5:31 AMHi,
there are numerous ways to compare dates, i give you some examples:
with data_Sample as
(select to_date('01-jan-2001','dd-mon-yyyy') d1, to_date('01-mar-2002','dd-mon-yyyy') d2, to_date('03-feb-2002','dd-mon-yyyy') d3 from dual)
select greatest(d1,d2,d3) from data_Sample
GREATEST(D1,D2,D3)
3/1/2002
with data_Sample as
(select to_date('01-jan-2001','dd-mon-yyyy') d1 from dual union all
select to_date('01-mar-2002','dd-mon-yyyy') from dual union all
select to_date('03-feb-2002','dd-mon-yyyy') from dual)
select max(d1) from data_Sample
MAX(D1)
3/1/2002
declare
d1 date :=to_date('01-jan-2001','dd-mon-yyyy');
d2 date :=to_date('01-jan-2001','dd-mon-yyyy');
begin
if d1>d2 then
dbms_output.PUT_LINE('d1 greater than d2');
else
dbms_output.PUT_LINE('d1 less or equql than d2');
end if;
end;
output:
d1 less or equal than d2now concerning your question:
>
can i use to_char to compare date
>
If you have date data types : don't convert them.
if you have no choice, use something like:
if to_char(d1,'YYYYMMDD') > to_char(d2,'YYYYMMDD') then
else
end if;Take care when you convert a date into a char format it becomes string comparison (with all it implies) (It's the reason why i've use an explicit mask format). -
Sql Developer Data Modeler 3.0 EA1: Custom transformation script
What is the object model used by:
Tools -> Design Rules -> Transformations
From looking at the examples (and guessing) I have managed to create ID attributes on all my entities, but how do I set its sequence number or set it as the primary key?
entities = model.getEntitySet().toArray();
for (var e = 0; e<entities.length; e++)
entity = entities[e];
att = entity.createAttribute();
att.setName('ID');
- Marc de Oliveiracontent of XML files:
1) class described, parent class if there is one
2)<property .../>
describes property - set and get method, data type of property, default value
3) <collection ... />
describes collection of objects belonging to class - 5 collections are defined in Table.xml - columns, indexes, table check constraints, column groups and spatial definitions
- get method - to get collection
- create item method - method of described class used to create item for that collection; so for columns collection table.createColumn() will create column in table
- other methods - add, move, remove if are defined
4)other meta data
definitions in 1) .. 4) are used by Oracle SQL Developer Data Modeler, so it'll be wrong to modify those xml files
5)<roproperty ../>
Data Modeler doesn't parse/process these definitions, we decided to not filter them out however you should not rely that much on them
6)some junk left
For each object you can set your own properties and they will be persisted (also included in compare/merge in versioning) together with other information for object - look at "template table" example in transformation scripts. Here are methods you can use:
void setProperty(String key, String value);
String getProperty(String key);
boolean hasProperty(String key);
boolean hasProperty(String key, String value);
void removeProperty(String key);
void clearProperties();
Iterator getPropertyNames();
Philip -
SQL Developer Data Modeler - 3000$???
The price is a joke, isn't it?
In which point is that tool better than the PL/SQL-Developer from Allround Automations for 180$?You're not comparing the same things.
Oracle SQL Developer is FREE - compare that with All Round Automations in terms of functionality.
Oracle SQL Developer Data Modeler is a separate tool. It's a separate product and is comparable to tools like CA Erwin Data Modeler and Embarcadero's ER Studio - which are certainly not free.
Sue -
SQL Developer Data Modeler - Version 3.0.0.665 - Open older project fail
Hi,
I just downloaded latest version of SQL Developer Data Modeler (3.0.0.665) and there is no "File" -> "Open" menu item. There is only "Save", "Save as", "Compare with" and "Exit".
I try to drag`n`drop project file to the Data Modeler window. This operation leads to opening project file as XML. I got the same result when I try to open the file with windows context menu "Open with".
How can I open older Data Modeler project? My previous version was 3.0.0 and project file was created there.
Maybe I need disable "Start Page'? But how? I cann`t find appropriate param in Tools -> PreferencesHi,
See this thread: Re: 30EA2 problem: Menu items not available
Ivan -
Best way to compare data of 2 tables present on 2 different servers
Hi,
We are doing data migration and I wil like to compare data between 2 tables which are present on 2 different server. I know to find the difference i can go for minus or full outer join and by creating the database link.
But my problem is the volume of the data. The tables under consideration has approximately 40-60 columns and number of rows in each tables are around 60-70 million. Also both the tables are on 2 diffferent servers.
I would like to know
1] What will be the best way to compare the data and print the difference from performance perepective? I know that if I am going for DB links then its will definitely impact the performance as the tables are across 2 different servers.
2] Is it advisable to go for using SQL - PL/SQL for this kind of senario or dump the data in flat files and use C or C++ code to find the difference.
Regards,
AmolCheck this at asktom.oracle.com. Search for "Marco Stefanetti" and follow the few posts between Marco and Tom. As far as your tables being on separate servers, you could consider dumping the data to file and using external table or using CTAS ( create table as select ) to get both tables locally.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2151582681236 -
hi all, the below code has one error in compare date : today >= expiredate. In this program, i want to compare if today is greater than expiredate, then status = overdue write to table named totalday. thanks
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:booking");
PreparedStatement ps1 = con.prepareStatement("select date from totalpay");
PreparedStatement ps2 = con.prepareStatement("update totalpay set status = 'overdue'");
GregorianCalendar calendar= new GregorianCalenda();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String today = df.format(calendar.getTime());
calendar.add(Calendar.DATE, 30);
String expiredate = df.format(calendar.getTime());
ResultSet rs = ps1.executeQuery();
while (rs.next()){
//int today1 = Integer.parseInt(today);
//int expiredate1 = Integer.parseInt(expiredate);
if (today >= expiredate)
ps2.setString(3,status);
ps2.executeUpdate();
rs.close();
ps1.close();
ps2.close();
con.close();
[\code]Thanks for your reply. it got no error, but the code changed status to 'overdue' at all fields of status include the date '30/04/2004. is the code something wrong??Thanks
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:booking");
PreparedStatement ps1 = con.prepareStatement("select date from totalpay");
PreparedStatement ps2 = con.prepareStatement("update totalpay set status = 'overdue'");
// milliseconds in a day.
final long DAY = 24L * 60 * 60 * 1000;
// date format of the SQL field.
final SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
// today as a date.
final long today = System.currentTimeMillis() / DAY;
// assuming the expiry is 30 days from today.
final long expiryDay = today + 30;
final ResultSet rs = ps1.executeQuery();
while (rs.next()) {
final String recDateString = rs.getString("date");
final long recDate = df.parse(recDateString).getTime() / DAY;
if (recDate >= expiryDay)
ps2.setString(3, status);
ps2.executeUpdate();
rs.close();
ps1.close();
ps2.close();
con.close();
JOptionPane.showMessageDialog(this,"Status updated.");
MainMenu mm = new MainMenu();
this.setVisible(false); //close frame
mm.setVisible(true);
[\code] -
Hi ,
Oracle 10.2.0.1.0
I need to compare date field with time stamp as well . I tried doing byt getting time stamp using to_char and on convertion into date by using to_date , I'm loosing time stamp please help804282 wrote:
I need to compare date field with time stamp as well Compare how exactly?
If you for example want to see if the date and timestamp are "equal", you can use the following approach - where in this specific approach it is deemed that if the difference between the timestamp and date is less than 1 sec, the two values are equal. E.g.
SQL>
SQL> create table test_tab(
2 d date,
3 t timestamp
4 );
Table created.
SQL>
SQL> insert into test_tab values( sysdate, systimestamp );
1 row created.
SQL> --// is the date and timestamp within 1 sec of one another?
SQL> select * from test_tab where t-d < to_dsinterval('0 0:0:1.00');
D T
2011/07/12 07:25:06 12/JUL/11 07:25:06.744832
SQL> --// the reverse test
SQL> select * from test_tab where t-d >= to_dsinterval('0 0:0:1.00');
no rows selected
SQL> -
Ignore time component when comparing dates
DB version:10gR2
In one of our codes , the client wants the dates to be compared by ignoring the time component from the date.
ie they don't want MM:DD:YYYY HH:MI:SS, they just want MM:DD:YYYY,
So I am modifying
when ship_date > est_date_del
to
when to_char(ship_date, 'YYYYMMDD')> to_char(est_date_del,'YYYYMMDD')
This is fine. Right? Will there be any issues relating to this comparision logic. You guys have any suggestions when comparing dates using to_char function?But if you use TRUNC without fmt , then date will be truncated to the nearest day. Right? Wouldn't that make the calculations wrong?
Sorry?
Here is one demonstration ->
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
Elapsed: 00:00:00.00
satyaki>
satyaki>
satyaki>with t
2 as
3 (
4 select to_date('01-OCT-08 01:30:00','DD-MON-RR HH24:MI:SS') res from dual
5 )
6 select to_char(res,'DD-MON-RR HH24:MI:SS') orig_dt,
7 to_char(trunc(res,'MONTH'),'DD-MON-RR HH24:MI:SS') cooked_dt
8 from t;
ORIG_DT COOKED_DT
01-OCT-08 01:30:00 01-OCT-08 00:00:00
Elapsed: 00:00:00.00
satyaki>
satyaki>Regards.
Satyaki De.
Maybe you are looking for
-
Is anyone aware of a leather holster that is big enough to accommodate the iPhone 4 with the wireless keyboard attached to it? Needs to be able to be clipped to a belt. Thank you.
-
I'm using Variable substitution for the Directory I determine dynamically using UDF. I'm gettig following error. Would someone clarify how to wrok around this error. 2006-09-28 14:43:38 EDT: Message processing failed: Error during variable substituti
-
Serious error in Acrobat 8, need to re-install, doesn't fix
Hi! I have Acrobat 8 running on Windows XP. It decided today to not work; no mods have been made to this program for a long time, no updates or anything weird! The error appears when I open Acrobat, but also if I open any folder containing a PDF. T
-
Scripting charts in illustrator
I work in the graphics department at the Financial Times. As you might imagine we generate a large amount of basic charts. We have a custome written javascript plugin called Ilex that takes an excel spreadsheet and given variables such as single colu
-
My ipod nano 1g is not working, it will be replaced?
my ipod nano 1g is not working, it will be replaced?