Date Conversion Issue MS Access to SQL Server - sql-server

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.

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.

Load the date separated by '/' with the Datetime datatype in sql server

INSERT INTO PUZ_DATE_FORMAT
SELECT FORMAT(GETDATE(),'d', 'it-IT') AS ItalianDate
I get this error:
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
My table contains only a single column of datetime datatype, and the format statement which I've written will give the output like 15/12/2017.
But when I try to insert a row into the table, it won't allow me to do so. It allows only dd-mm-yyyy format - not dd/mm/yyyy. Why?
Basically what you are trying to do is insert a string value to a Datetime field. In doing so, SQL Server try to cast the string to a datetime during the insert. If your string is in the Universal format (yyyy-MM-dd) it can successfully parse it without any issue. Else the string has to be in the datetime format as Server's culture.
I guess your Server's culture is en-US which has the date time format as "MM/dd/yyyy". With regarding to your date (15/12/2017) server thinks 15 is the month, 12 is the day and so on. Which is obviously falling.
If you tried this before 12th of the month, It can successfully cast, But to an incorrect value.
Further, you are not doing the right thing by, casting a Datetime to a string and then try to insert in to Datetime field. Try to store the raw Datetime in database and format accordingly when displaying in UIs.
Cheers,

SQL Server convert datetimeoffset to timestamp

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

Unable to retrieve date columns in hive

I am finding difficulty when it comes to retrieving date values in hive. My query is
create external table test1(DISPLAYSCALE int, CREATED_DATE date, LAST_EDITED_DATE date)
ROW FORMAT SERDE 'com.esri.hadoop.hive.serde.JsonSerde' STORED AS INPUTFORMAT 'com.esri.json.hadoop.UnenclosedJsonInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';
When I try to use the select * from test1 limit5 I get this error;
Failed with exception
java.io.IOException:java.lang.ClassCastException:
org.apache.hadoop.hive.serde2.io.DateWritable cannot be cast to
org.apache.hadoop.io.Text
As per the json the datatype for CREATED_DATE and CREATED_DATE are esriFieldTypeDate and the values are in this format say 2013-11-20 09:39:25.000001.
So i used the date datatype while creating the table, copied it to HDFS using the unenclosed json and used the select * query to retrieve the columns, but I get the above error. To get the values we are creating the same table with string data type respectively instead of date and we are able to get the values .
Can you suggest a solution for this problem. This question may seem silly but I am pretty new to programming.
Date data type supports only format YYYY-­MM-­DD
Timestamps or VARCHAR data type you can be used rather than date data type.
Date data types do not exist in Hive. In fact the dates are treated as strings in Hive. Refer the below post
http://www.folkstalk.com/2011/11/date-functions-in-hive.html
EsriJsonSerDe (and GeoJsonSerDe) support for DATE and TIMETAMP type columns is added on Spatial-Framework-for-Hadoop master in git.
Alternately, you can try using org.openx.data.jsonserde.JsonSerDe or org.apache.hive.hcatalog.data.JsonSerDe (instead of EsriJsonSerDe - and with column type string rather than binary) together with UnenclosedEsriJsonInputFormat.
[disclosure: Spatial-Framework-for-Hadoop collaborator]

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