I'm trying to get the current time of a remote server running SQL Server 2012 from a client machine. I have this SQL code:
Select SYSDATETIME() as ServerTime
I thought this was exactly what I wanted and since I was using a development machine with my own SQL Server, it worked fine because I never thought the time I got was my own computer time. Then I found out this doesn't work if I wanted the remote machine's date/time. Is there anyway I can get the remote server's date/time when my application starts up? Thank you.
SELECT 'SYSDATETIME' AS FunctionName, SYSDATETIME() AS DateTimeFormat
UNION ALL
SELECT 'SYSDATETIMEOFFSET', SYSDATETIMEOFFSET()
UNION ALL
SELECT 'SYSUTCDATETIME', SYSUTCDATETIME()
UNION ALL
SELECT 'CURRENT_TIMESTAMP', CURRENT_TIMESTAMP
UNION ALL
SELECT 'GETDATE', GETDATE()
UNION ALL
SELECT 'GETUTCDATE', GETUTCDATE()
UNION ALL
SELECT 'SYSUTCDATETIME', SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
Where +5:30 is IST
Output:
Sql Working Fiddle
Holy cow I got it to work everyone...It turned out I have a default value of the connection string. I also have a .ini file that contains the server's connection string. And I ended up using the default connection string. The rest of my SQL worked because I've reestablished the server's connection string after getting the server's time. I apologize for all the trouble I've caused. I never wanted to ask this but I just couldn't find the fix until now. Thank you for all your help.
Related
Is there any way to change SQL server datetime? GETDATE() and CURRENT_TIMESTAMP show the wrong dates.
I read that SQL reads the date from the server settings where the SQL instance is installed. But on this server(Windows SERVER 2019) time, date and timezone are correct.
Only in SQL Server are those settings wrong. I've tried to find a solution for this issue over internet but I couldn't find any.
Only SYSUTCDATETIME() shows correct information.
GETDATE() and CURRENT_TIMESTAMP will be working correctly. They return the server's local time. If the time is "wrong" then it's because the time on the server is "wrong"; most likely because it thinks it's in a different timezone to where it physically is or has the wrong DST setting.
Clearly, however, the time is correct on the server for its location (in the sense of that if it's observing CET it would display 11:38 around now) as you state that SYSUTCDATETIME() returns the correct UTC time.
if the server does have the wrong time setting, however, the fix is fix the time on the server. Speak to your server administrator about that. YOu change change the values GETDATE() and CURRENT_TIMESTAMP return as they are based on the host's time. This is why I am confident the time is correct, likely it is either set to the wrong timezone or DST setting as the UTC time is correct.
If, however, the time is correct for where the server is physically located, then the answer is don't use GETDATE() or CURRENT_TIMESTAMP to get the value for your local time, instead (like you have) use SYSUTCDATETIME() or SYSDATETIMEOFFSET() and then convert the time to your timezone in the application layer.
It seems the server timezone was changed since the SQL Server instance was started. Run the query below to verify the timezone SQL Server is currently using:
SELECT CURRENT_TIMEZONE();
If the result is different than the OS configuration, restart the SQL Server instance for the new timezone to become effective.
I've been using #DBCommand on MySQL db's. Great success. Now I have to switch to SQL Server.
I manage to update the SQL Server database just fine (via Domino).
The problem occur when I try to generate a report for the web user (Domino).
The query works like a charm in SQL Server Management Studio. Running the web page yields:
Data Access Application Layer>Could not execute #db function
The original query worked just fine against MYSQL.
I am in need of some resources/documentation/web page describing hints and tips in order to make it work. Any ideas?
Example query that fails (when using Domino):
SELECT COUNT( bruk_NR ) as [antall], [brukere_Navn], [bruk_NR]
FROM [stat].[dbo].[bruk]
INNER JOIN
[stat].[dbo].[brukere]
ON bruk.bruk_NR = brukere.brukere_NR AND bruk.bruk_NR = brukere.brukere_NR
GROUP BY bruk.bruk_NR, brukere.brukere_Navn, brukere.brukere_brukerID
ORDER BY [brukere_brukerID] ASC
Thanks in advance
Hal.
What command could be used to discover the last datetime SQL Server was restarted?
SELECT MIN ([login_time]) FROM sysprocesses;
Source:
"What is the current uptime of SQL Server?"
If you talking about Microsoft's SQL Server 2005 or above, MSDN's docs about sys.databases says:
create_date
Date the database was created or renamed. For tempdb, this value changes every time the server restarts.
So, this should be what you need:
SELECT create_date FROM sys.databases WHERE NAME='tempdb'
Following is the error message I get when I tried to execute Select * for the table in SQl Server, Any suggestions would be appreciated.
"lock request time out period exceeded
sql server"
Look at SQL hints, in particular WITH (NOLOCK)
http://www.developerfusion.com/article/1688/sql-server-locks/4/
When you do you SELECT you're locking the table. But your SELECT takes so much time, that SQL ends it, in order not to keep the table locked, mainly because SQL server thinks it's a bug.
I have to include one report in my application showing offline/online activity of few databases on SQL Server 2008.
Could you please suggest how can I collect teh same information from sql server?
SELECT DATABASEPROPERTYEX('YOURDATABASE', 'Status')
DatabaseStatus_DATABASEPROPERTYEX
GO
SELECT state_desc DatabaseStatus_sysDatabase
FROM sys.databases
WHERE name = 'YOURDATABASE'
GO
This will tell you the status of the database.
In order to find out when your database was taken OFFLINE, you can use the SQL that I posted before, or the easiest way is to check the Event Viewer and it will tell you when the Database was taken OFFLINE. I have just tested this on my local machine and SQL Server writes out an Information message to the Application log.
You can also use below query to check database status.
SELECT Name, state_desc FROM sys.databases