I've been storing date times in local time and I need to convert them all to Universal time.
How can I do this in SQL Server 2008?
By local time, I mean either BST or GMT.
i.e. I stored DateTime.Now() from .NET throughout the year, and I now need to homogenise the data.
Thanks!
please consider sending already from the c# the utc
also, there is SELECT GETUTCDATE()
in sql server
http://msdn.microsoft.com/en-us/library/ms178635.aspx
edit
if you need to update so
UPDATE SomeTable
SET DateTimeStamp = DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), DateTimeStamp)
Use the DATEADD function of SQL Server. You just have to know to no. of hours difference from your local to time to UTC.
Related
When I get TODAY in SELECT in PB, it returns 1900/1/1
date var
Select TODAY() Into :var From DUMMY
But when I assign to variable TODAY(), it works as expected
date var
var = today()
I use MS SQL Server 2016 and PowerBuilder 12.5.
I've supposed that the problem is in different date formats, but I have changed date format at my Windows locale in the way, that PB TODAY() returns 2018-10-08 and MSSQL GetDate() returns 2018-10-08 18:25:23.207
So date parts have the same formats.
The problem is not in DUMMY table since I have created MS SQL DUMMY table and inserted 1 row in it.
Also I'm wondering if there are any difference in SELECT TODAY() and var = TODAY()?
I suppose that 1st variant returns MS SQL server time but 2nd returns local time. Is not is?
Try below SQL.
Select getdate() into :var From DUMMY;
You provided your own answer: Today() is a PowerScript function, GetDate() is the function on MS SQL. If you’re executing SQL, it needs to be a valid SQL statement for the server you’re executing against (except for the INTO :var part), and can’t include a PowerScript function.
Two other things:
“FROM DUMMY” is an Oracle thing, and I’m pretty sure it won’t work on MS. (You’re capturing your error codes after executing the SQL, right?)
I won’t say this is likely a critical problem, but as you point out, GetDate returns a datetime; I’d recommend that as your data type for the capture variable.
And yes, GetDate() will be your server’s date/time, Today() will be based on the local workstation.
Good luck.
I'm very new to sql so im not sure how i would go about changing the line below to take in account for the new year. The sql server query i'm running will run on January 1st of 2014. Will this work in sql server 2008 or will it need to be changed for the new year to capture 2013?
DT.[DOS-DATE] between convert(date,GETDATE()-7) and convert(date,GETDATE())
This query takes records between the last 7 days and today. The year does not matter in that condition.
It's better to use explicit operators on date. Also, no reason to call GETDATE() twice. Try this instead:
DECLARE #Now DATETIME = GETDATE()
...
DT.[DOS-DATE] between convert(date,DATEADD(day, -7, #Now)) and convert(date,#Now)
However, technically your existing code will work.
Also, you may want to take a look at this for potential issues with using BETWEEN:
What do BETWEEN and the devil have in common?
if any date time value provided to sql server can i get it's midnight value with some function in sql server.. for example if i provide 2013/07/03 01:34AM , i want to get it to 2013/07/03 12:00 AM.Is there a way to do it?
SQL Server 2008+
SELECT CAST(CAST('2013/07/03 01:34AM' AS date) AS datetime)
For older versions, see this Best approach to remove time part of datetime in SQL Server Never use anything that requires float or int or varchar conversions
This should give you what you need:
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, InputDateField), 0)
Should be slightly quicker than cast:
Most efficient way in SQL Server to get date from date+time?
Does anyone know how can I default the time for the date with SQL Server?
Example:
When I use getdate() and it will return me the current date and time. How can I get the current date but default the time portion to '10:00:00.000' ?
This works for SQL Server (SQLFiddle):
SELECT DATEADD(hour, 10, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
Explanation: GETDATE() gives current date and time. Casting to DATE makes it date only (midnight time). Casting it again to DATETIME makes it compatible with DATEADD, which finally adds 10 hours to it.
Use the below if you are using sql server.
select cast(cast(getdate()AS INT)+0.41667 as datetime)
Note time is scaled on a 0.0 to 0.9999, and the no. of hours are equally distributed. e.g. 0.5 will give 12a.m.
This is from DB2. But same concept should work very DB. Just convert that date in to Timestamp
SELECT TIMESTAMP(CURRENT_DATE, TIME('10:00:00')) AS MY_DATE FROM SYSIBM.SYSDUMMY1
in sql server 2008 and above:
select convert(datetime,convert(varchar(10),convert(date,(GETDATE())))+' 00:00:00')
select convert(datetime,convert(varchar,convert(date,getdate())) + ' 10:00:00.000')
SQL FIDDLE
Again, in Ms SQL Server, You can also use
Select DateAdd(day, datediff(day, getdate()), 0) + 10/24.0
I'm currently using
Convert(varchar, Getdate(), 101)
to insert only date part of system date into one of my sql server database tables.
my question is: is it the right way to do that or is there any other better method to do it?
I don't understand why you're converting the GETDATE() output (which is DATETIME already) to a VARCHAR and then SQL Server would convert it back to DATETIME upon inserting it again.
Just use:
INSERT INTO dbo.YourTable(SomeDateTimeColumn)
VALUES(GETDATE())
If you're doing that conversion to get rid of the time portion of the DATETIME, you should better:
use the DATE datatype (available in SQL Server 2008 and newer) to store only the DATE (no time)
if you're using SQL Server 2005 or earlier, use this conversion instead - should be much more efficient than two conversions!
INSERT INTO dbo.YourTable(SomeDateTimeColumn)
VALUES(DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
Update: did some performance testing, and in this particular case, it seems the amount of work that SQL Server needs to do is really the same - regardless of whether you're using the convert to varchar stripping the time and back to datetime approach that you already have, or whether you're using my get the number of days since date 0 approach. Doesn't seem to make a difference in the end.
The BEST solution however would still be: if you only need the date anyway - use a column of type DATE (in SQL Server 2008 and newer) and save yourself any conversions or manipulations of the GETDATE() output altogether.