I was working on migrating data from MYSQL to HSQL.
In MYSQL data file, there are plenty of records where date values are set as '0000-00-00' and HSQL database throws below error:
"data exception: invalid datetime format / Error Code: -3407 / State:
22007"
for all such records.
I would like to know what could be optimum solution for this problem?
Thanks in advance
HSQLDB follows the SQL Standard and allows valid dates only. A date such as '0001-01-01' would be a good candidate for the default value.
Regardless of the method used for data inserts, the '0000-00-00' strings should be corrected before insert. One way of doing this is to use a default value for the target column with DEFAULT DATE'0001-01-01' and replace the string in the INSERT statement with the keyword DEFAULT. For example:
CREATE TABLE MYTABLE ( C1 INT, C2 DATE DEFAULT DATE'0001-01-01')
INSERT INTO MYTABLE VALUES 1, DEFAULT
INSERT INTO MYTABLE VALUES 3, '2010-08-14'
Related
I am trying to run unit tests for my SQL Server query. Query is simply inserting date to the table. I tried two different formats but didn't work:
parameters.addValue(STUDY_DATE, getDate(studyEvent.getStudy().getStudyDate()));
Timestamp timestamp = new Timestamp(getDate(studyEvent.getStudy().getStudyDate()).getTimeInMillis());
parameters.addValue(STUDY_DATE, timestamp);
And this is getDate() method that returns Calendar object:
private Calendar getDate(long time) {
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"));
calendar.setTimeInMillis(time);
return calendar;
}
I am not sure if the problem is that SQL Server's datetime format issue or hsqldb issue. Here is my hsqldb create table:
SET DATABASE SQL SYNTAX MSS TRUE;
DROP TABLE event_study IF EXISTS;
CREATE TABLE event_study
(
STUDY_ID INT,
STUDY_DATE DATE
)
Is my hsqldb setup wrong? or should I use different datetime format for SQL Server?
Error I am getting is:
data exception: invalid datetime format; nested exception is java.sql.SQLDataException: data exception: invalid datetime format
and SQL query that I am running is:
INSERT INTO event_study(study_id, study_date)
SELECT x.*
FROM (VALUES(:study_id, :study_date))
AS x(study_id, study_date)
WHERE NOT EXISTS (SELECT 1 FROM event_study s WHERE s.study_id = x.study_id)
As you are not using strings for dates, this is not actually a formatting issue, but a Java type issue. With your table definition, DATE does not have time information. You can create and use a java.sql.Date object for the parameter value. If you want a datetime column, which includes information on time of the day, then use TIMESTAMP in your table definition and a java.sql.Timestamp for the parameter value.
In either case, you cannot use a Calendar object as parameter value.
I have a datetimeoffset column DateEntry in my SQL Server table. When I want to convert it to a timestamp format with this query :
SELECT CAST(Table1.[DateEntry] AS timestamp)
FROM Table1
I get the following error :
Error : 529- Explicit conversion from data type datetimeoffset to
timestamp is not allowed.
TIMESTAMP in SQL Server has absolutely nothing to do with a date and time, therefore you cannot convert an existing date&time into a TIMESTAMP.
TIMESTAMP or more recently called ROWVERSION is really just a binary counter that SQL Server updates internally whenever row has been modified. You cannot set a TIMESTAMP column yourself, you can just read it out. It is used almost exclusively for optimistic concurrency checks - checking to see whether a row has been modified since it's been read, before updating it.
According to MSDN:
The timestamp data type is just an incrementing number and does not
preserve a date or a time. To record a date or time, use a datetime
data type.
If your are absolutely sure, you can use indirect conversion:
DECLARE #dto datetimeoffset = '2016-01-01 12:30:56.45678'
SELECT CONVERT(timestamp, CONVERT(varbinary(12), #dto))
See also #marc_s's answer.
Try the following script if this this is what you are trying your side
SELECT CAST(CAST(Table1.[DateEntry] AS datetime) as timestamp) FROM Table1
I'm creating a table B from an exisitng table A. In the table A I have a column ValDate which is varchar and contains Date. When I try to create the table B I have a function used in the query as given below and I get a conversion error. Table A contains null values as well.
MS Access:
((DateDiff("d",Date(),format(Replace(Replace([Table A].ValDate,".","/"),"00/00/0000","00:00:00"),"dd/mm/yyyy")))>0)).
Tables were in MS Access and are being migrated to SQL Server 2012.
SQL Server:
((DATEDIFF(day,FORMAT( GETDATE(), 'dd-MM-yyyy', 'en-US' ),FORMAT( ValDate, 'dd-MM-yyyy', 'en-US' ))>0))
or
((DateDiff(day,GETDATE(),Format(Replace(Replace([TableA].[ValidFrom],'.','/'),'00/00/0000','00:00:00'),'dd/mm/yyyy')))
I tried converting the date using several approachs like Convert , Format and Cast but I end up getting error as below.
Msg 8116, Level 16, State 1, Line 1
Argument data type date is invalid for argument 1 of isdate function.
I would really appreciate someone telling me what I'm missing here.
since you have date data in a string field is very likely you have some value that is not valid against your expected date format.
copy the data in a sql server table and then perform check and validation of the content of the string field.
have a look to the function try_convert that can be helpful when checking the content of the string field containing the date values.
when bad data is ruled out you can apply again your formula with (hopefully) a different result.
a better solution would be to create a separate field with appropriate datatype to store date values converted from the string field and apply your logic to that field.
I am importing some data from excel to db, the issue is I want to set the specific default value like 00/00/0000 to datetime column when there is no date available from the Excel file.
The getdate() sets the date to current one, but I want to set to a specific date, is it possible to achieve something like this.
Sure you can set a default value - but be aware: DATETIME has a valid range from 1/1/1753 through 12/31/9999 - so setting it to 0/0/0000 will NOT be a valid DATETIME value!
If you need such a value - use either DATETIME2 in SQL Server 2008 (range is from 1/1/0001 through 12/31/9999) - or use DATE (without any time - same range as DATETIME2)
Or: just make your DATETIME/DATETIME2/DATE column nullable and insert NULL when no date is present - that would be the cleanest solution.
When you use bcp or BULK INSERT or SQLBulkCopy, then you have the option of keeping NULLs.
If you don't, then you get the default for the column.
See "Keeping Nulls or Using Default Values During Bulk Import" on MSDN
If you can't (say because you get empty string not NULL) then you can use a a staging table first, then load the data from that with a combination of NULLID, ISNULL and CASE. Or use a trigger, but this will slow you down more so than doing the load in 2 steps
I'm not sure how you are performing your import, but if you wish to check whether a value is NULL and set it to a defined value then you can use the SQL IsNull(a,b) where a is the value to check and b is the value to return.
insert into [dbo].[tblSomeTable]
set [someField] = IsNull(#thisValueMayBeNull, thisValueIfNull)
Probably not a good example but should point you in the right direction.
Or once your data has been imported you could run an update script to replace all NULL dates with a fixed value;
Or you could set the date field in the database to NOT NULL and give it a defaut value.
I have 2 databases in SQL Server 2005.
I want a functionality that i have same table structure in 2 database for example i have a same table named as testData in 2 database named as dbTest1 and dbTest2.
Now i want a single query through which i can add all the records from table testData of database dbTest2 into table testData of database dbTest1.
I tried to use following query
insert into dbTest1.testData values select * from dbTest2.testData
but this query is not running and giving error.
I also tried
insert into dbTest1.testData(col1,col2,col3) values select * from dbTest2.testData
but this also gives error that "Invalid object name dbTest2.testData"
Could any one help in this
Thanks
Replace dbTest2.testData with dbTest2..testData - you have to specify 3 things (or optionally leave the middle blank for dbo).
i.e.
insert into dbTest1..testData
select * from dbTest2..testData
If the table doesn't already exist in dbTest1, you can do this:
select *
into dbTest1..testData
from dbTest2..testData
You need to specify all column names in query.
insert into dbTest1.dbo.testData(col1,col2,col3) select * from dbTest2.dbo.testData