I am passing param of type XML as a stored procedure param succesfully. I have no problems handling all the data from XML, but I discovered that dates from XML are not retrieved properly.
For example, 2013-03-14T15:14:53.598438+01:00 is now 2013-03-14 14:14:00. It seems that SQL is calculating +1 to display time.
I am using following T-SQL to get date from xml
select
A.B.value('xs:dateTime((ConfirmationDate)[1])', 'smalldatetime') as ConfirmationDate
FROM
#XML.nodes('/ArrayOfPreOrder/PreOrder/confirmationinfo/ConfirmationInfo') A(B)) as ConfirmationDate
How can I correct this issue? Both server and SQL Server are in the same timezone.
Check if there is a difference between your timezone setting and the timezone you're importing...
SELECT SYSDATETIMEOFFSET()
DECLARE #TimeZone NVARCHAR(255)
EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'SYSTEM\CurrentControlSet\Control\TimeZoneInformation',
N'TimeZoneKeyName',
#TimeZone OUTPUT
SELECT #TimeZone
You're specifying the timezone in the XML you're importing so I'm guessing it is converting it to local time for you. So your timezone would be +2 where you're importing from +1.
Related
In SQL server, is there any query to get the windows format Time zone name of current server like India Standard Time or US Eastern Standard Time.
By Using SELECT SYSDATETIMEOFFSET() , it's getting Time zone offset value but requirement is to get Time zone name.
Found this on SQL Authority
DECLARE #TimeZone VARCHAR(50);
EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
'SYSTEM\CurrentControlSet\Control\TimeZoneInformation',
'TimeZoneKeyName',#TimeZone OUT;
SELECT #TimeZone as 'Current TimeZone';
I have Stored Procedure on SQL to get the time the database last updated. When O run the Query by itself, I get Month dd yyyy hh:mm AM/PM.
But when O execute stored procedure, I get yyyy-MM-dd hh:mm:ss
I need the Month dd yyyy hh:mm AM/Pm ( Apr 1 2022 7:30AM) format.
What Am I doing wrong in converting the datetime.
ALTER PROCEDURE [dbo].[spGetDBLastUpdatedTime]
-- Add the parameters for the stored procedure here
#LastUpdatedTime Datetime OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SET #LastUpdatedTime =(SELECT
convert(Varchar(MAX),last_user_update,100) as LastUpdatedTime
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Emp')
AND OBJECT_ID=OBJECT_ID('tblEmployee'))
RETURN;
END
Real Datetime values DO NOT HAVE A STRING FORMAT AT ALL. What you see is a convenience shown you by the tool or environment where you ran the query. The actual value is a binary format. It's not really human-readable at all, but rather is more efficient for storage, transport, indexing, and operations like comparing or adding arbitrary days, months, milliseconds, etc.
If you need a specific format, still return a raw (unformatted) DateTime value from your database query, and then use the string conversion tools on whatever platform or reporting tool you're working with to get the desired format there, in the presentation level where it belongs.
I'm currently migrating my system to Azure SQL so I need to convert the time zone from the UTC standard to the local time zone when using GETDATE(). This is straight forward with 'AT TIME ZONE..', however we also have a couple of clients who host themselves on machines running SQL Server pre-2016.
Is there any way in SQL that one function could be setup to get the time zone that'd work for both?
In Azure SQL Database, the regional settings of the database are set to UTC by default. It is also advisable to store dates and times in UTC format on our on-premises SQL Server instances, and handle all time zone calculations at the presentation layer.
Sometimes though, it may be necessary to query data directly and see what the local date and time is, or calculate the date and time in another region. For this use-case we can use the AT TIME ZONE hint, which was introduced in SQL Server 2016.
For computers running pre-SQL Server 2016, you can make you date function query which database engine is running and then use GETDATE() for those SQL Server versions.
SELECT SERVERPROPERTY ('edition')
Here it is, please see Alberto Morillo's answer for reference.
DECLARE #CurrentDateTime DATETIME,
#RawSQL NVARCHAR(1000)
IF ((SELECT CAST(SERVERPROPERTY('Edition') AS SYSNAME)) = 'SQL Azure')
BEGIN
SET #RawSQL = 'SET #CurrentDateTimeOUT = CONVERT(DATETIME, CONVERT(datetimeoffset, GETDATE()) AT TIME ZONE ''GMT Standard Time'') '
END
ELSE
BEGIN
SET #RawSQL = 'SET #CurrentDateTimeOUT = GETDATE() '
END
EXEC sp_executesql #RawSQL, N'#CurrentDateTimeOUT datetime OUTPUT', #CurrentDateTimeOUT=#CurrentDateTime OUTPUT
SELECT #CurrentDateTime
I want the query to return yesterdays records and be flexible in the future if there is a need to reload x Days. Therefore project parameter DaysToReload is Int32 and value is set to -1.
Source query looks like this:
SELECT * FROM State.vStateHourly S
WHERE S.DateTime >= DATEADD(d, convert(int, ?), DATEADD(d,0,DATEDIFF(d,0,GETDATE())))
This works as expected in multiple environments, but on one site results are really strange.
When running SQL Profiler I have found that it is checking for data type of S.DateTime column before executing the above query. The event before source query execution is:
set fmtonly on select S.DateTime from State.vStateHourly S where 1=2 set fmtonly off
After this SSIS seems to set the ? parameter to data type DateTime, since the following event in trace is:
declare #p1 int
set #p1=5
exec sp_prepare #p1 output,N'#P1 datetime',N'SET FMTONLY OFF;
select top 10 *
FROM
State.vStateHourly S
WHERE S.DateTime >= DATEADD(d, convert(int, #P1), DATEADD(d,0,DATEDIFF(d,0,GETDATE())))',1
select #p1
Which is followed by:
exec sp_execute 1,'1899-12-29 00:00:00'
I can not observe this behaviour in any other Environment that is available to me.
Source SQL-server version: 10.50.6529.0
SSIS Server version: 13.0.1601
Any ideas what might be causing this data type lookup and how I could stabilize the behaviour?
I got this working by moving the
DATEADD(d, convert(int, ?), DATEADD(d,0,DATEDIFF(d,0,GETDATE())))
part of the WHERE statement into a DateTime variable. The variable expression looks like this:
DATEADD("day", #[$Project::DaysToReload] , DATEADD("day", DATEDIFF("day",(DT_DBDATE)("1900-01-01"), GETDATE()), (DT_DBDATE)("1900-01-01")))
It is using the global DaysToReload parameter to create a DateTime value. Then I mapped this variable to the original Query which ended up looking like this:
SELECT * FROM State.vStateHourly S
WHERE S.DateTime >= ?
Now SQL profiler shows that the DB Engine is checking type of S.DateTime field and then runs the Query to compare it to datetime type variable. Result is all rows starting with midnight the day before.
I searched but wasn't able to find the way to get the date in this format (DD.MM.YYYY)
Help me please change this request:
DECLARE #date datetime
set #date = '01.05.2016'
SELECT [User], cast(DATEADD(SECOND, sum(datediff(DAY, #date,[Start])),#date) as date)'Date'
,cast(DATEADD(SECOND, sum(datediff(SECOND, '00:00:00',[Period])),'00:00:00') as time)'Total time'
FROM [Table].[TableAction]
where
[Start] >= #date+'00:00:00' and [Start] <= #date+'23:59:59'
group by [USER]
DECLARE #date datetime set #date = GETDATE()
Now to output it, you need to "Format" it.
select FORMAT (#date,'MM.dd.yy') as date
The best practice is to store the datetime in datetime format in the database and whenever you need the data you can access it and format it according to your need.
DECLARE #Currentdate DATETIME;
SET #Currentdate=GETDATE(); -- Store cuurent date into variable
And then when you want to display it use the below to format it as dd.MM.YYYY
SELECT CONVERT(VARCHAR(10),GETDATE(),104); -- format the #Currentdate to the required format.
FORMAT works only in SQL Server 2012+. If your database is SQL server 2008 or 2005 FORMAT doesn't work.In that case, you can go for the CONVERT function.
So, If your database is above SQL SERVER 2012, you can go for FORMAT as suggested by Tschallacka
DECLARE #Currentdate DATETIME=GETDATE(); -- Store cuurent date into variable
And then when you want to display it use the below to format it as dd.MM.YYYY
SELECT FORMAT(#Currentdate,'dd.MM.yyyy') -- format the #Currentdate to the required format.