How can i get client system current datetime from sql instance? - datetime

Sql server getdate() function fetch server current datetime, how can I get client system current datetime from sql server instance?.
One of our client using one database for multiple companies operating from different time zones, being a huge database with many tables, procedures, functions and the getdate() function is used as default value for many area.

You can't. The SQL Server doesn't know anything about the time where the client that called it is located.
Consider using getutcdate() instead. Use UTC exclusively in your database, then convert between time zones in the application layer.
If you feel you need to convert between time zones in the database itself, you will need a third-party solution, such as my SQL Server Time Zone Support package.

I have same problem. I found this link:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/290a0085-47a5-48ab-9557-1b59ee269a40/call-getdate-from-linked-server?forum=transactsql
declare #rmt_time datetime;
exec('SET ? = CURRENT_TIMESTAMP', #rmt_time OUTPUT) at [remote_server];
select CURRENT_TIMESTAMP as local_time, #rmt_time as rmt_time;
Hope this helps.

Related

Efficient method to move data from Oracle (SQL Developer) to MS SQL Server

Daily, I query a few tables in SQL Developer, filtering to prior day activity, adding column to date stamp the data, then export to xlsx. Then I manually import each file to a MS SQL Server via SQL Server Import and Export Wizard. Takes many clicks, much waiting...
I'm essentially creating an archive in SQL Server, the application I'm querying overwrites data daily. I'm not a DBA of either database, I use the archived data to do validations and research.
It's tough to get my org to provide additional software, I've been trying to make this work via SQL Developer, SSMS Express ed, and other standard tools.
I'm looking to make this reasonably automated, either via scripts, scheduled tasks, etc. Appreciate suggestions that would work on my current situation, but if that isn't reasonable, and there's a very reasonable alternative, I can go back to the org to request software/access/assistance.
You can use SSIS to import the data directly from Oracle to SQL Server, unless you need the .xlsx files for another purpose. You can also export from Oracle to these, then load to SQL Server from these files if you do need the files. For the date stamp column, a Derived Column can be added within a Data Flow Task using the SSIS GETDATE() function for a timestamp in order to achieve the same result. This function returns a timestamp, and if only the date is necessary the (DT_DBDATE) function can cast it to a date data type that's compatible with this data type of SQL Server. Once you have the SSIS package configured, you can schedule in to run at regular intervals as a SQL Agent job. I'd also recommend installing the SSIS catalog (SSISDB) and using this the source to run the packages from. The following links shed more light on these areas.
SSIS
Connecting to Oracle from SSIS
Data Flow Task
Derived Column Transformation
Creating SQL Server Agent Jobs for SSIS packages
SSIS Catalog
Another option that you may consider (if it is supported in SQL Express) is using the BCP utility, which can be run from command line.
The BCP utility allows you to bulk copy the data from a delimited text file into a SQL Server table.
If you go this approach, things to consider:
Number of Columns in the source file need to match the number of columns in the destination
Data types must match (or be comparable)
Typically, empty strings will be converted to nulls, so you will need to consider if the columns are nullable.
(to name a few - if you want to delve deeper, you might also need to look at custom delimiters between fields and records. Don't forget, commas and line feeds are still valid characters in char type fields).
Anyhow, maybe it will work for you, maybe not. Sure, you might still have to deal with the exporting of the data from Oracle, but it might ease the pain getting the data in.
Have a read:
https://learn.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-2017

How to transfer data from SQL Server to Informix and vice versa

I want to transfer tables data from SQL server to Informix and vice versa.
The transferring should be run scheduled and sometimes when the user make a specific action.
I do this operation through delete and insert transactions and it takes along long time through the web between 15 minute to 30 minute.
How to do this operation in easy way taking the performance in consideration?
Say I have
Vacation table in SQL Server and want to transfer all the updated data to the Vacation table in Informix.
and
Permission table in Informix and want to transfer all the updated data to the Permission table in SQL Server.
DISCLAIMER: I am not an SQL Server DBA. However, I have been an Informix DBA for over ten years and can make some recommendations as to its performance.
Disclaimer aside, it sounds like you already have a functional application, but the performance is a show-stopper and that is where you are mainly looking for advice.
There are some technical pieces of information that would be helpful to know, but in their absence, I'm going to make the following assumptions about your environment and application. Please comment or edit your question if I am wrong on any of these.
Database server versions. From the tags, it appears you are using SQL server 2012. However, I cannot determine the Informix server and version. I will assume you are running at least IDS 11.50 or greater.
How the data is being exchanged currently. Are you connecting directly from your .NET application to Informix? I would assume that is the case with SQL Server and will make the same assumption for your Informix connection as well.
Table structures. I assume you have proper indexing on the tables. On the Informix side, dbschema -d *dbname* -t *tablename* will give the basic schema.
If you haven't tried exporting data to CSV and as long as you don't have any compliance concerns doing this, I would suggest loading the data from a comma-delimited file. (Informix normally deals with pipe-delimited files, so you'll either need to adjust the delimiter on the SQL Server side to a pipe | or on the Informix import side). On the Informix end, this would be a
LOAD FROM 'source_file_from_sql_server' DELIMITER '|' INSERT INTO vacation (field1, field2, ..)
For reusability, I would recommend putting this in a stored procedure. Just wrap that load statement inside a BEGIN WORK; and COMMIT WORK; to keep your transactional integrity. Michał Niklas suggested some ways to track changes. If there is any correlation between the transfer of data to the vacation table in Informix and the permission table back in SQL Server, I would propose another option, which is adding a trigger to the vacation table so that you write all new values to a staging table.
With the import logic in a stored procedure, you can fire the import on demand:
EXECUTE PROCEDURE vacation_import();
You also mentioned the need to schedule the import, which can be accomplished with Informix's "dbcron". Using this feature, you'll create a scheduled task that executes vacation_import() periodically as well. If you haven't used this feature before, using OAT will be helpful. You will also want to do some housekeeping with the CSV files. This can be addressed with the system() call, which you can make from stored procedures in Informix.
Some ideas:
Add was_transferred column to source tables setting its default value to 0 (you can use 0/1 instead of false/true).
From source table select data with was_transferred=0.
After transferring data update selected source row, set its was_transferred to 1.
Make table syncro_info with fields like date_start and date_stop. If you discover that there is record with date_stop IS NULL it will mean that you are tranferring data. This will protect you against synchronizing data twice.

Which date and time will insert into SQL Server Database if I wrote in sql query as Getdate() for createDate field?

If I published my Asp.Net solution on a server and server date time is correct.
Now, When any user access that URL from LAN network on another PC and that PC date time is not correct.
In that case if I write
Insert Into tblComp(1,'XYZ','Jam',GETDATE()) in my query.Then which date time will be inserted.Server or PC(which have accessing that URL)
tblComp structure
********************
id int,
SName varchar(50),
SAdd varchar(50),
CreateDate Datetime
*********************
The query executes on the database server, the database server will provide its current date and time.
Please note that this date and time will be in whatever time zone the database server is in as well.
From the documentation of GETDATE():
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.
If, on the other hand, you actually want the local date and time of the client, you will have to provide that through a parameter. On an ASP.NET site, this is not all that easy because the "client code" will the ASP.NET application, running on the web server, so instead of the date and time of the database server you would effectively use the date and time of the web server.
To actually get the client date and time (that is, the local date and time of the computer running the web browser that is browsing the site), you would probably have to resort to javascript, though I'm completely unfamiliar with how you would do that.

SQlite3 — SQL datetime stamps

Am I right in thinking that SQL does intrinsically store a date/time stamp for each commit? i.e., that I have to allow for recording this information as part of my schema design?
It will take a certain amount of space to store this information explicitly (using CURRENT_TIMESTAMP or my own timestamp), and if there's a way of accessing comparable information in some internal database setting, I'd do that instead. I'm working with SQLite3 at the moment.
SQLite does not have any internal time stamps.
(SQLite database files do not even have any record of transaction once they are committed.)

Store GMT dates on a US server

I'm from the UK, and have recently deployed a website on WinHost's Basic Package.
When using DateTime.Now() in C#, or GETDATE() in SQL, these are both returning something like GMT-8 (because the server is hosted in the US).
I think I'm a bit limited in terms of permissions on the server (for example I can't change my SQL Login Language).
What is the best method of storing these dates in GMT?
use DateTime.UtcNow and store as it is database.
When reading from database assume UTC and convert into UK time using TimeZoneInfo class.
Note that the SQL should not contain any information about time zone offsets.
In SqlServer use getUtcDate() to store all your datetime values. You can convert it to required timezone in your .NET application.
Description
I think you should store your DateTimes in UTC. Then you can simple convert it to another Timezone with the TimezoneInfo class.
Save DateTime.UtcNow to your database or use getUtcDate inside sql
Let the user choose his timezone and save them to to your database
Convert the UTC Time you have saved to the users timezone using TimeZoneInfo.ConvertTimeFromUtc
Sample
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(/* destination timezone (users timezone) */);
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(YourDateTimeFromDatabase, cstZone);
More Information
How to do timezones? In asp.net mvc
MSDN - TimeZoneInfo Class

Resources