SQL Server - data lost when converted datetime to varchar - sql-server

I had to restore a table that contained a datetime column.
I used bulk insert to insert the data from a CSV file. However the import couldn't insert the datetime values because SQL server saw it as a different format.
I ended up modifying the table, removing the datetime data type and replacing it with a varchar.
The issue is the data got converted from this format: 7/15/2015 3:41:57 PM to something like this: 47:47.0
Is there a way I can convert these values back or is the data lost?

As #ChrisSteele mentioned, your data is hosed. It likely got this way by Excel's cool feature to convert datetime strings to integers. Try re-saving the original file in notepad, or changing the format of the column from Date/Datetime to Text if you're using Excel.

Related

Importing Flat File into SQL Server stores incorrect dates In the database

I am a SQL student who has been tasked with loading data into SQL Server for a company that I intern with.
I have tried loading multiple flat files with dates formatted as 1/23/04 into SSMS and when going through the wizard the dates preview correctly. Once they're loaded into the actual database and a select query is performed, all dates return as 2023-01-04 format.
I'm not sure where to even begin to fix this. I've loaded columns as nvarchar(50) as opposed to date, datetime, and datetime2 to see if it would make a difference, and each case returns the same format. Is this a setting in the flat file, SSMS, or the computer itself?
In SSIS bring in the column (with the dates) as a string and add a derived column transform that will transform the column (using the substring function) to the correct date. SQL Server loves seeing dates as YYYY-MM-DD so that is why without explicitly telling it how to read the string it defaults to thinking that the inputted date is of that format.
If you are using SQL Server (SSMS) you should input it as a string (char(8)) and then use cast or convert functions to change the string into a date. You can then issue a 'Alter table drop column' to drop the string version column of the date.

SSIS Convert m/dd/yyyy to yyyymmdd with inconsistencies

I'm loading many files into a SQL SERVER database. I have one flat file that has a Date Column coming in as string[DT_STR].
I have TWO "date fields" in my database. One is varchar, one is datetime.
Converting the datetime column is no issue, I just use Data Conversion/Derived Column if necessary. However, this varchar column is giving me trouble. Our database values for this column should be in yyyymmdd format. However, on this single file the format of the dates change.
Normally I'd do a SUBSTRING(...) expression here, but the difficulty is that the format of these dates change. some examples of values could be
08/16/2017
8/16/2017
08/6/2017
08/06/2017
10/6/2017
10/06/2017
This makes the challenge harder. I tried LEN([DATE]) == NUM_HERE ? do_THING : OTHER_CALC, but this approach failed because the length of 10/6/2017 is the same as 8/06/2017 which will give me the wrong result. Does anyone have a good workaround for this?
Perhaps a simple convert to date and then into the final format. If 2012+, use try_convert() to trap any bogus dates.
Example
Declare #YourTable Table ([SomeCol] varchar(50))
Insert Into #YourTable Values
('08/16/2017')
,('8/16/2017')
,('08/6/2017')
,('08/06/2017')
,('10/6/2017')
,('10/06/2017')
Select *
,Formatted = convert(varchar(8),convert(Date,SomeCol),112)
from #YourTable
Returns
SomeCol Formatted
08/16/2017 20170816
8/16/2017 20170816
08/6/2017 20170806
08/06/2017 20170806
10/6/2017 20171006
10/06/2017 20171006
Convert the varchar data to datetime and convert that to a formatted string
SELECT CONVERT(varchar,(CONVERT(datetime, '8/6/2017')),112)

Geography Data Import

I'm trying to import geography data from a CSV file in to SQL server using the bcp (bulk copy tool) but I can't figure out the format. I would like to use something like this in the CSV files but I get errors on the POINT column:
101932694,POINT(44.0586891174316 -69.2067337036133 4326),2014-07-30,24452353
How can I format geography data in the CSV file so bcp will accept it?
OK, it could not find any information on importing human readable geography data using bcp, so I converted the point data:
POINT (44.058689117431641 -69.206733703613281 4326),...
To the binary form:
E6100000010D000000203B4D51C000000020830746400000000000E6B040,...
And put the binary form in my CSV file. bcp seemed to work fine with that.
I don't know anything about BCP, but hopefully this will give you what you need.
Try importing your data to a temporary table as a string, then importing it into the real table with an update.
Inside of SQL Server spatial data is stored like a varbinary, i.e. your point will be stored as
0xE6100000010C010000203B4D51C0FAFFFF1F83074640
If you try and move the data with SSIS, it actually treats it as varbinary, not as spatial. Because of this, a conversion is required to get your string into the correct format.
SELECT GEOGRAPHY::STGeomFromText('POINT(44.0586891174316 -69.2067337036133)', 4326)
or
SELECT GEOGRAPHY::Point(-69.2067337036133,44.0586891174316,4326)
For those reasons, I am guessing BCP does not implicitly recognize the conversion, and you will probably need to do it manually.
CSV => Temp Table =>
INSERT INTO RealTable (GeogColumn) SELECT GEOGRAPHY::STGeomFromText(GeogString,4326) FROM TempTable

SQL DATETIME Insert from Excel?

So im having a rather strange problem, I have a Column (lets say Column A) in excel that has data that looks like this:
4/11/2015 10:14
I have a bunch of other columns, but anyways in my SQL Insert statement within excel, the data (when copying out) looks like this:
42105.4561921296
The ="INSERT INTO TABLE VALUES ('"&A1&"', Etc....)" is in the data format of "general" and the Date column is in the format of "Custom" where there is a M/DD/YYYY MM/HH type within.
The SQL Column is of the data type DATETIME, so it of course doesn't accept the weird number it gets.
Any ideas? changing the format of the "SQL INSERT" column doesn't change the results.
You are right - Excel formats only changes the way the numbers are displayed, not the underlying value of the cell. In this case, the value of the cell is an excel date-time value, which is the # of days since 1/1/1900, with decimals for the time.
I'd recommend using Excel's TEXT function to convert Excel's numeric date-time value to a text string that can be inserted into SQL:
Instead of:
INSERT INTO TABLE VALUES ('"&A1&"', Etc....)"
Try:
INSERT INTO TABLE VALUES ('"&TEXT(A1,"YYYY-MM-DD HH:MM")&"', Etc...)"
The best format to insert a date time into a datetime column is to present the date and time as YYYY-MM-DD Minute:Second or 2015-04-15 12:52
so to insert a datetime from Excel you can use this set of Excel functions:
(where A1 contains the datetime to be saved)
=YEAR(A1)&"-"&RIGHT("00"&MONTH(A1),2)&"-"&RIGHT("00"&DAY(A1),2)&" "&RIGHT("00"&HOUR(A1),2)&":"&RIGHT("00"&MINUTE(A1),2)&":"&RIGHT("00"&SECOND(A1),2)

Exception when using TSQL type "Date" with Mono

I'm using Mono 2.10.8.1 on Ubuntu 12.04 Server.
I'm using an ADO.net TableAdapter to grab data from SQL Server 2008. When I encounter a Date column, Mono gives the following error:
No mapping exists from SqlDbType Date to a known DbType.
I'm not entirely sure what Mono uses for DB access (FreeTDS/etc) so I'm not 100% sure where to even start my search for a solution.
An obvious solution would be to simply change the column in the DB to DateTime, but since it is in production I do not have that option.
Has anybody else encountered this error before?
Thanks
In your TableAdapter SQL statement, try casting or converting the field being returned as a date to a datetime or, if necessary, to a varchar field with the necessary formatting. You can achieve this by doing the following:
Select field1
, field2
, CAST(date_field as datetime) as New_datetime_field
, CONVERT(varchar(10),date_field,101) as New_varchar_field --stored as MM/DD/YYYY string
From Table
Doing this will now cause the field returned by the query or stored procedure to be recognized as SQL as a datetime or varchar field. It should then be passed on using the TableAdapter as a datetime or varchar field. Using the convert statement, you can convert a date into any number of formats (see here for more information).

Resources