I am importing data from postgres into sql server. Fields where data type is 'timestamp without (or with) time zone' i used datetimeoffset in sql table but couldn't create package with the same. So i changed the datatype as datetime.
Now, when I import data,all the dates and time are coming perfectly alright but where there is no data ( blank) then its displayed as some weird date instead of NULL in my SQL table.
Those weird dates are -
1899-12-30 00:00:00.000
1753-01-01 00:00:00.000
What am i missing and how to resolve this?
Maybe that's a simple thing but I am quite new to SQL so apologies if its really easy but any help is appreciated.
Thanks,
AP
Suppose your meaningful data is from 2000 onwards, then run a update statement like:
update table
set column = NULL
where column < cast('01-01-2000' as datetime)
Related
I have been having some challenges preserving precision on insertion of a date using the Import/Export Wizard and losing the precision of my date time field. The sub-seconds times are always being lost on the second datetime field.
I am using MS SQL Server 2017. What am I missing / doing wrong to get the Import to work properly?
Ideally I want to create a DTS process to automate this but right now just trying to get the basic import working reliably.
My Raw data looks like this:
2020-12-29 14:59:59,036418016717,2020-12-29 15:00:00.0628
2020-12-29 15:00:00,000000004249,2020-12-29 15:00:00.0661
2020-12-29 15:00:00,000000005472,2020-12-29 15:00:00.1448
2020-12-29 15:00:00,000000001344,2020-12-29 15:00:00.1449
My Insert Table for these fields Looks like this:
([INCDATETIME] datetime NOT NULL,
[SEQNO] bigint,
[LOGDATETIME] datetime2(4) NOT NULL
The first date imports just fine being a date[DT_DATE] using the SQL Server Import and Export Wizard. For the second field I have tried both date[DT_Date], string(24), and the other Date Time formats available.. I have also tried defining it as a string in the table and then turn it back into a datetime as a calculated field but it is very inefficient to do so for what I need.
The best case out of all of them is my second date is always imported as "2020-12-29 15:00:00.0000" zeroing out the sub-seconds which I absolutely need for my purpose as I need to compare several fields like it.
I am pretty sure I need to use a DateTime2 as a destination field and even in this little test script it comes out correctly. So why is it not working with the Wizard?
DECLARE #mydate date = CONVERT(date,#mytime);
DECLARE #mydatetime2 datetime2(4) = CONVERT(datetime2,#mytime);
SELECT #mytime as 'String',
#mydate as 'Date',
#mydatetime2 AS 'Datetime2'
String Date Datetime2
------------------------ ---------- ---------------------------
2020-12-29 15:00:00.9494 2020-12-29 2020-12-29 15:00:00.9494
I am exporting data from an existing sql server 2012 table by using the ssms option database right click -> Tasks -> generate scripts. This works fine except for date columns.
Example output:
INSERT INTO Employees VALUES (1, 'Frank', ,CAST(0x16190B00 AS Date));
I Don' find a way to export date columns to a format like 2.10.2012. I can think about adding a string column and cast the date column values as a string, but this is just a work around.
Can someone help me out here?
Like Mikael said, this is a bug which will be fixed in the next major release of SQL Server
Sl,No Name BirthDay
1 Jojin1 2013-05-12 00:00:00.000
2 jojin2 2012-06-12 00:00:00.000
3 jojin3 2015-04-12 00:00:00.000
I have table called Datefunction. In that Birthday in above display format but I need DD/MM/YYYY. I try to convert below Query but Updating successful message, but check with Table not happening changes
UPDATE DateFunction
SET BirthDay = CONVERT(VARCHAR(15),BirthDay,103)
Firstly, I completely agree with Diamond GreezeR and Damein.
But having said that i know OP can't implement those things right now.
What you can do?
ALTER Table temp
ALTER column Datecolumn varchar(15)
update temp
set Datecolumn=CONVERT(VARCHAR(15),getdate(),103)
You cannot store the dates in that format, you can only display them.
SELECT CONVERT(varchar(10, BirthDay, 103)
FROM [Datefunction]
Other options (from my comments below):
You could create a text field and store the result of the CONVERT function. See Luv's answer: https://stackoverflow.com/a/16646539/909882
Another possibility is to use the DATEFORMAT option in SQL Server. However, I think this only sets the format for the session; it does not change it permanently.
Also, you could could use SET LANGUAGE to choose the date format that SQL Server expects in queries. See stackoverflow.com/questions/1187127/… as an example
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.
Very weird problem occurred, I have moved a site from one server to another - All is working, but any query involving a date is playing up. I get the following:
DELETE FROM MYTABLE WHERE categoryId = -2 AND datecreated < '3/23/2010';
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Now what's strange is I have changed the LCID to 1033 on the new server as the date is showing as US format and its still throwing an error! I then tried 2057 and again the same error? Made no difference.
I'm a little confused, as this is a working site from a server with IIS6 - The locale is 1033 on that server and it works perfectly!! :S
I have just tried thrown a Cdate() around the date too and yet again the same error???
Any ideas??
Well, I use to express datetime varchar fields in the yyyyMMdd format, and have not had problems with that
AND datecreated < '20100323';
As far as I know, formatting the dates as 'dd-mmm-yyyy', with mmm beging the three character English name for the month, dd being the number and yyyy obviously being the year, works with every database I have worked with (except some French Oracle db which needed the French month name).