how do I get date time including milliseconds in VBA - sql-server

I have a VBA macro ( excel vba ) where I have some code querying a SQL Server database field. The field is of type datetime
The field has values including milliseconds.
if I query the field with ADO and the Recordset will give me teh field value formatted and without the milliseconds bit
for example the actual field value when queries in SQL management studio
is
2015-12-14 10:19:48.077
but the recordset value is
14/12/2015 10:19:48 AM
How can I get the actual value ?

Try casting as string. Like This in SQL Server.
select CONVERT(nvarchar(50), GETDATE(), 113) as MyDateAsStringWithMS
Casting a date as string is useful in scenarios where your are converting between SQL and ORACLE and MySQL and ODBC level libraries like in Java and VBA and COM.
I chose 113 on purpose. I have had a lot of success using that format when I want milliseconds.

Related

MS Access: How to specify a ODBC Connection String with country specific date queries?

I have severe troubles to set up an MS access application that uses linked tables to an SQL Server 2012 Database.
The problem is that SQL Queries have problems to interpret German dates: e.g. "31.12.2019" doesn't work, "01.01.2019" works. So I suspect that it is a problem with localization. E.g.
select * from table where date >= [Forms]![someForm]![fromDate]
[Forms]![someForm]![fromDate] is a string in a form, edited by a date picker.
I was able to solve the problem by using the ODBC Microsoft SQL Server Setup Wizzard, and selecting "Ländereinstellungen verwenden" (engl. use country specific settings).
(Sorry the following screenshot is in German).
I would like to specify this in a classic ODBC connection string: e.g
DRIVER=ODBC Driver 13 for SQL Server;SERVER=.\SqlExpress2012;Trusted_Connection=Yes;APP=Microsoft Office;DATABASE=suplattform;?country-specific=yes?
However I did not find such an parameter in any documentation. Is this possible?
Best regards
Michael
Also, specify the data type of the parameter - and date is a reserved word in Access SQL:
parameters [Forms]![someForm]![fromDate] DateTime;
select * from table where [date] >= [Forms]![someForm]![fromDate]

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.

Format date of filters and results on SQL Server

I'm in a trouble with the format of dates on my queries.
In SQL Server when I execute the following query:
select *
from users
where register_date >= '2015-03-17'
It throws me a cast conversion error.
But if I do execute the following query:
select *
from users
where register_date >= '2015-17-03'
It returns me the correct data, BUT when I see the the register_date column, it gives me the dates as 'YYYY-MM-DD' format...so it's a little confusing...
How can I configure SQL Server to work always with "YYYY-MM-DD" format, on filters and results?
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.
The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.
There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.
The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.
The ISO-8601 format is supported by SQL Server comes in two flavors:
YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!
or:
YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.
Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.
The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
So in your concrete case - what if you run this query:
select *
from users
where register_date >= '20150317'
Do you get the expected results?
Your Date format in SQL SERVER is 'YYYY-DD-MM' that's why you are getting cast conversion error. For searching you must give the date with this exact format.
But In case of getting result you can use SQL DATE FORMAT and retrieve date in any format e.g
SELECT DATE_FORMAT(users.register_date ,'%Y-%d-%m') AS date FROM users
Result will be : 2015-17-03

Date Conversion Issue MS Access to 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.

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