SQL Server - Add seconds to time field - sql

I need to add 8 seconds to a time (e.g. 15:49:12.8080000), but its stored as varchar.
How do I go about it? Any help is appreciated.
Thank you.

here is one way of doing that if you have 2008+ -
update yourtable set timecolumn = dateadd(s, 8, cast(timecolumn as time))
from tbl
see sqlfiddle - http://www.sqlfiddle.com/#!6/efdad/2

If you need to do it for few rows, than you can do by just manually updating, column is varchar:
assuming original value was : 15:49:12.8080000
update table set time_column = "15:49:20.8080000" .....

Related

SQL Server datetime Format Isn't Working

TimeOpened is a column in my dataase table of type datetime2.
My convert statement is:
convert(nvarchar, TimeOpened, 114)
My output is supposed to be just the time (24 hours based) without the date, i.e.
5/16/2016 1:38:00 PM --> 13:38:00
but in practice the output I get is 01:38:00
Why is this happening?
Additionally I would like to know how to remove the seconds. Basically I want my output to be 13:38.
Thanks.
Try this (SQL Server 2012 or later):
SELECT FORMAT(TimeOpened,'dd/MM/yyyy HH:mm:ss')
For sql server 2008 you can cast to time and then convert to char(5):
SELECT CONVERT(char(5), CAST(TimeOpened As Time))
SELECT FORMAT(cast(TimeOpened as time),N'hh\.mm') as mytime
you can see more examples on https://msdn.microsoft.com/en-us/library/hh213505.aspx#ExampleD
try this
SELECT LEFT(CONVERT(VARCHAR,TimeOpened,108),5)
Solved it.
The problem was that when I inserted the date I used c# datetime.now instead of getdate()

SQL Server 2008 - convert varchar to datetime column

I have a table with a varchar column (dateandtime) with date and time in the format dd/mm/yyyy hh:mm:ss and I need to convert it to a datetime column.
I have a temp column dateandtimetemp in the table .
Thanks
Try this:
SELECT CONVERT(Datetime, '15/05/2013 13:55:12', 104)
It should return : 2013-05-15 13:55:12.000
Try
SELECT CONVERT(VARCHAR(30),GETDATE(),113) ;
it return result in following format
15 May 2013 16:26:29:850
So then just go ahead and try it!
UPDATE dbo.YourTable
SET DateAndTimeTemp = CAST(DateAndTime AS DATETIME)
and see if it works. If your input data is really always properly defined - you should have no issues here.
This of course depends on what langauge/dateformat setting you have activated in your database - so that might be the first problem you encounter.
If you do have issues, then you can always "clean up" your input data and try again ...
Try this
SELECT CONVERT(DATETIME, '15/05/2013 11:12:13', 103)

SQL Server: Add seconds to a datetime field?

This should be a softball for you SQL guys. I know I can add to an int field with something like UPDATE tblUser SET Total=(Total+2) but what is the syntax for adding seconds to a datetime field?
I'm using SQLServer 2008
UPDATE tbluser SET DateField = DATEADD(ss,numOfSeconds,DateField)
Note the first parameter "ss". This shows that you are adding seconds to the date.
Check the docs for more info.
You should look into DATEADD.
DATEADD (datepart , number , date)
or the full update syntax
UPDATE tbl SET YourDateField = DATEADD (ss, 2, YourDateField)

Update only Time in a mysql DateTime field

How can I update only the time in an already existing DateTime field in MySQL? I want the date to stay the same.
Try this:
UPDATE yourtable
SET yourcolumn = concat(date(yourcolumn), ' 21:00:00')
WHERE Id = yourid;
Try this:
UPDATE t1 SET DateTimeField = CONCAT(DATE(DateTimeField),' 12:34:56');
UPDATE myTable
SET myDateTime = ADDTIME(DATE(myDateTime), #myTimeSpan)
WHERE id = #id;
Documented on MySQl date functions MySQL docs
I have solved in this way:
UPDATE table
SET myDateTime = CONCAT_WS(' ',DATE(myDateTime), CURTIME())
WHERE id = #id;
Obviously you should change CURTIME() with your desired time.
UPDATE myTable
SET myDateTime = ADDTIME(myDateTime, #myTimeSpan)
WHERE id = #id;
For exact syntax of function, see this.
Try this:
UPDATE sms
SET entry_period_end_date= entry_period_end_date+INTERVAL 6 Hour
WHERE TIME(entry_period_end_date) = '06:00:00';
UPDATE `table`
SET time = ADDTIME(time, INTERVAL 13 Hour);
Well, exactly what you are asking for is not possible. The date and time components can't be updated separately, so you have to calculate the new DateTime value from the existing one so that you can replace the whole value.
MySQL DEV page shows functions like subtime and difftime
A sample code to back the time all posts in 3 hours is above:
UPDATE tablepost SET datepost = SUBTIME( datepost , '0 3:0:0' );
Note that values 0 dont alter the respective field. Take care this code, use select first to test these function.
Reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime
Asuming you have a DATE field and TIME field and want to inject the time into the date, try this:
UPDATE mytable
SET mydatefield = ADDTIME( DATE_FORMAT(mydatefield,'%Y-%m-%d 00:00:00'), mydatefield)
WHERE myid = ...
I used ADDTIME in the following way
Earlier in my cloud server, the DateTime was set to UTC but after changing the DateTime to Asia/Kolkata ie UTC 5:30 I wanted the same to reflect in my database tables.
I wanted to update the created_at and updated_at column by 5 hours 30 minutes. I did the following
To update all the rows of the table
UPDATE
products
SET
created_at = ADDTIME(created_at, '5:30:0'),
updated_at = ADDTIME(updated_at, '5:30:0')
You can omit the WHERE condition if you want to update all the records, but since my new records were updated with proper values. So only my rows below id less than 2500 must be updated
UPDATE
products
SET
created_at = ADDTIME(created_at, '5:30:0'),
updated_at = ADDTIME(updated_at, '5:30:0')
WHERE
id < 2500;
This what helped me. I convert time to minutes firstly: 150 for 2:30 am.
UPDATE lesson SET starts_at = DATE_ADD(Date(starts_at), INTERVAL 150 MINUTE)
Minutes are enough accurate for me, though you can use other units: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add

Datetime in where clause

How can I select 12/20/2008 in where clause of sql?
The server is SQL server 2005.
select * from tblErrorLog
where errorDate = '12/20/2008'
WHERE datetime_column >= '20081220 00:00:00.000'
AND datetime_column < '20081221 00:00:00.000'
First of all, I'd recommend using the ISO-8601 standard format for date/time - it works regardless of the language and regional settings on your SQL Server. ISO-8601 is the YYYYMMDD format - no spaces, no dashes - just the data:
select * from tblErrorLog
where errorDate = '20081220'
Second of all, you need to be aware that SQL Server 2005 DATETIME always includes a time. If you check for exact match with just the date part, you'll get only rows that match with a time of 0:00:00 - nothing else.
You can either use any of the recommend range queries mentioned, or in SQL Server 2008, you could use the DATE only date time - or you could do a check something like:
select * from tblErrorLog
where DAY(errorDate) = 20 AND MONTH(errorDate) = 12 AND YEAR(errorDate) = 2008
Whichever works best for you.
If you need to do this query often, you could either try to normalize the DATETIME to include only the date, or you could add computed columns for DAY, MONTH and YEAR:
ALTER TABLE tblErrorLog
ADD ErrorDay AS DAY(ErrorDate) PERSISTED
ALTER TABLE tblErrorLog
ADD ErrorMonth AS MONTH(ErrorDate) PERSISTED
ALTER TABLE tblErrorLog
ADD ErrorYear AS YEAR(ErrorDate) PERSISTED
and then you could query more easily:
select * from tblErrorLog
where ErrorMonth = 5 AND ErrorYear = 2009
and so forth. Since those fields are computed and PERSISTED, they're always up to date and always current, and since they're peristed, you can even index them if needed.
You don't say which database you are using but in MS SQL Server it would be
WHERE DateField = {d '2008-12-20'}
If it is a timestamp field then you'll need a range:
WHERE DateField BETWEEN {ts '2008-12-20 00:00:00'} AND {ts '2008-12-20 23:59:59'}
Assuming we're talking SQL Server DateTime
Note: BETWEEN includes both ends of the range, so technically this pattern will be wrong:
errorDate BETWEEN '12/20/2008' AND '12/21/2008'
My preferred method for a time range like that is:
'20081220' <= errorDate AND errordate < '20081221'
Works with common indexes (range scan, SARGable, functionless) and correctly clips off midnight of the next day, without relying on SQL Server's time granularity (e.g. 23:59:59.997)
Use a convert function to get all entries for a particular day.
Select * from tblErrorLog where convert(date,errorDate,101) = '12/20/2008'
See CAST and CONVERT for more info
select * from tblErrorLog
where errorDate BETWEEN '12/20/2008' AND DATEADD(DAY, 1, '12/20/2008')
Use below query for clear understanding
/****** Script for SelectTopNRows command from SSMS ******/
SELECT *
FROM [dbo].[PublishedInfo]
where PublishedDate >= '2022-02-14T11:31:16.5299166+00:00'