How to add an hour in timestamp in sql server (without Declare) - sql-server

I want to add an hour in the parameter TimeStamp, but not with declare parameter i.e
DECLARE #datetime2 datetime2 = '2019-03-01T09:25:21.1+01:00'
SELECT DATEADD(hour,1,#datetime)
I have a column name TimeStamp in a table and i want to add in all data plus 1 hour.
The column
TimeStamp
2019-03-01T09:25:20.1+01:00
2019-03-01T09:25:21.1+01:00
2019-03-01T09:25:19.1+01:00
I try something like this
SELECT DATEADD(hour,1, TimeStamp), but i have an error
Conversion failed when converting date and/or time from character
string.
Any possible answers ??
Thanks

SELECT DATEADD(hour,1, TimeStamp) is correct
However, The format in TimeStamp is wrong,
So, cast it to DateTime2 First
CAST(TimeStamp as DateTime2)
OR
CAST('2019-03-01T09:25:20.1+01:00' as DateTime2)
So,
SELECT DATEADD(hour, 1, CAST(TimeStamp as DateTime2))

Conversion failed when converting date and/or time from character
string.
The error message means that column TimeStamp stored as a string. DATEADD expects a valid value that is date/datetime/datetime2 or can be converted into it from a string. Because a sample value look like DATETIME2, such extra conversion perhaps is needed:
SELECT DATEADD(hour,1, CAST(TimeStamp as datetime2))

Your syntax will be fine as defined.
It might be a value in your column that is not able to parse to datetime2 because it contains an invalid character.
You could add the ISDATE() to the expression to check if it is valid.
https://learn.microsoft.com/en-us/sql/t-sql/functions/isdate-transact-sql?view=sql-server-2017
edit: forgot to mention you could parse before adding with try_cast or try_convert to datetime2

In Your Timestamp +01:00 represents the Time offset to GMT. You can convert this to your local time and then Add the Hours using DATEADD()
or Remove the Time Offset from the string and add one hour using DATEADD() As suggested by Others.

According to this https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017#date-and-time-styles
you have to convert the timestamp with timezone using type 127.
127 is the input format for:
ISO8601 with time zone Z.
yyyy-mm-ddThh:mi:ss.mmmZ (no spaces)
Note: For a milliseconds (mmm) value of 0, the millisecond decimal value will not display. For example, the value '2012-11-07T18:26:20.000 will display as '2012-11-07T18:26:20'.
select convert(datetime2, '2019-03-01T09:25:20.1+01:00', 127)
if you are not using convert and the 127 by using cast you may run in conversion problems depending on language settings of the users.

Maybe you are after this?
select dateadd(hour,1,convert(datetimeoffset, TimeStamp))
Best to not store dates and times as text though.
Edit: Note that his will retain your time zone information if that is important to you.

Related

Error converting '2021-05-30 19:00:00+00' into SQL Server datetime

I am trying to convert a varchar column containing dates like 2021-05-30 19:00:00+00 to datetime before inserting it into a datetime column in another table.
SELECT CAST('2021-05-30 19:00:00+00' AS datetime)
But I get this exception
Exception: Conversion failed when converting date and/or time from character string
datetime values don't have an offset. Assuming that your value, '2021-05-30 19:00:00+00', is coming from a variable/column then truncate the offset from the value, and then use an explicit style:
SELECT CONVERT(datetime,LEFT('2021-05-30 19:00:00+00',19),121);
If you want to maintain the offset, add the minutes:
SELECT CONVERT(datetimeoffset(0),'2021-05-30 19:00:00+00'+':00',121);
Problem with the above cast is the Time Zone. if you need to change the Time Zone then your code will have to use CONVERT_TZ.
/* works with mysql 5+ */
select CAST('2021-05-30 19:00:00' as DATETIME);
Using a substring to remove the time zone and cast it to
select cast((SUBSTRING_INDEX('2021-05-30 19:00:00+00','+',1)) as DATETIME);
you can also use convert the date time to the system time zone using CONVERT_TZ but why would you change the time zone to what your system is using,
SELECT CAST(CONVERT_TZ(CAST(SUBSTRING_INDEX('2021-05-30 19:00:00+00','+',1) AS DATETIME),CONCAT('+',CONCAT(SUBSTRING_INDEX('21-05-30 19:00:00+00','+',-1),':00')),##session.time_zone) AS DATETIME);

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

How to add 1 day from a user defined date in SQL?

What I understood in SQL is if you add 1 day from today's date, you can use
SELECT #date = CONVERT(VARCHAR(8), DATEADD(DAY, 1, GETDATE()), 112)
but my issue is, I am not getting today's date. I have a user defined field for date which I was the one who set this date.
Example: my column is name is "Return Booked", how shall I add 1 day to every return booked I have set?
See attached: SQL Server result
Also, if I try to use CONVERT (DATEADD, *****) I have an error below:
How to deal with the error: Conversion failed when converting date and/or time from character string.
My current SELECT statement is
SELECT dbo.AdditionalDetailInfo.UserDefined6 AS ReturnBooked
Note that the data type of UserDefined6 is nvarchar. :(
Thank you!
You need to change the GETDATE() in the snippet to your value, and it is not a good practice to keep date values in nvarchar, so you will need some conversion first.
something like that:
SELECT CONVERT(VARCHAR(8), DATEADD(DAY, 1, (CONVERT(DATETIME,UserDefined6))),112) AS ReturnBooked
FROM dbo.AdditionalDetailInfo
As datatype of your column is nvarchar, Dateadd function on nvarchar will fail, so First you need to convert the column value to datetime and then use the DateAdd function, like below
Select DATEADD(DAY, 1, CONVERT(Datetime,ReturnBooked))
After looking at the image of sample data, it seems you have a row with date text in wrong format 08/010/2018. The conversion will fail for this particular row, so recommended to fix such bad data.

TSQL Datediff from a string

I have a simple query that I am trying to use this select statement on.
I am trying to get the difference in seconds from the date provided (a UNIX timestamp).
select
DATEDIFF(s, '19700101', CAST(CAST('2014-08-27 08:59:56.0000000' AS DATETIME) AS INT))
When I try this, I get the following error :
Conversion failed when converting date and or time from character string.
I figured casting the string as datetime would have resolved that?
First of all, you should try to avoid using shorthands for the datepart, instead of s, use SECOND.
Another good practice when casting a string to a date is to use CONVERT so you can specify the format to use on the conversion (in your case, it would be 121. Finally, for the whole thing to work, you'll need to use less miliseconds (up to 3):
DECLARE #Date VARCHAR(50)
SET #Date = '2014-08-27 08:59:56.0000000'
SELECT DATEDIFF(SECOND,'19700101',CONVERT(DATETIME,LEFT(#Date,23),121))

How to convert SQL Server's timestamp column to datetime format

As SQL Server returns timestamp like 'Nov 14 2011 03:12:12:947PM', is there some easy way to convert string to date format like 'Y-m-d H:i:s'.
So far I use
date('Y-m-d H:i:s',strtotime('Nov 14 2011 03:12:12:947PM'))
SQL Server's TIMESTAMP datatype has nothing to do with a date and time!
It's just a hexadecimal representation of a consecutive 8 byte integer - it's only good for making sure a row hasn't change since it's been read.
You can read off the hexadecimal integer or if you want a BIGINT. As an example:
SELECT CAST (0x0000000017E30D64 AS BIGINT)
The result is
400756068
In newer versions of SQL Server, it's being called RowVersion - since that's really what it is. See the MSDN docs on ROWVERSION:
Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism
for version-stamping table rows. The
rowversion data type is just an incrementing number and does not
preserve a date or a time. To record a date or time, use a datetime2
data type.
So you cannot convert a SQL Server TIMESTAMP to a date/time - it's just not a date/time.
But if you're saying timestamp but really you mean a DATETIME column - then you can use any of those valid date formats described in the CAST and CONVERT topic in the MSDN help. Those are defined and supported "out of the box" by SQL Server. Anything else is not supported, e.g. you have to do a lot of manual casting and concatenating (not recommended).
The format you're looking for looks a bit like the ODBC canonical (style = 121):
DECLARE #today DATETIME = SYSDATETIME()
SELECT CONVERT(VARCHAR(50), #today, 121)
gives:
2011-11-14 10:29:00.470
SQL Server 2012 will finally have a FORMAT function to do custom formatting......
The simplest way of doing this is:
SELECT id,name,FROM_UNIXTIME(registration_date) FROM `tbl_registration`;
This gives the date column atleast in a readable format.
Further if you want to change te format click here.
Using cast you can get date from a timestamp field:
SELECT CAST(timestamp_field AS DATE) FROM tbl_name
Works fine, except this message:
Implicit conversion from data type varchar to timestamp is not allowed. Use the CONVERT function to run this query
So yes, TIMESTAMP (RowVersion) is NOT a DATE :)
To be honest, I fidddled around quite some time myself to find a way to convert it to a date.
Best way is to convert it to INT and compare. That's what this type is meant to be.
If you want a date - just add a Datetime column and live happily ever after :)
cheers mac
My coworkers helped me with this:
select CONVERT(VARCHAR(10), <tms_column>, 112), count(*)
from table where <tms_column> > '2012-09-10'
group by CONVERT(VARCHAR(10), <tms_column>, 112);
or
select CONVERT(DATE, <tms_column>, 112), count(*)
from table where <tms_column> > '2012-09-10'
group by CONVERT(DATE, <tms_column>, 112);
"You keep using that word. I do not think it means what you think it means."
— Inigo Montoya
The timestamp has absolutely no relationship to time as marc_s originally said.
declare #Test table (
TestId int identity(1,1) primary key clustered
,Ts timestamp
,CurrentDt datetime default getdate()
,Something varchar(max)
)
insert into #Test (Something)
select name from sys.tables
waitfor delay '00:00:10'
insert into #Test (Something)
select name from sys.tables
select * from #Test
Notice in the output that Ts (hex) increments by one for each record, but the actual time has a gap of 10 seconds. If it were related to time then there would be a gap in the timestamp to correspond with the difference in the time.
for me works:
TO_DATE('19700101', 'yyyymmdd') + (TIME / 24 / 60 / 60)
(oracle DB)
Robert Mauro has the correct comment. For those who know the Sybase origins, datetime was really two separate integers, one for date, one for time, so timestamp aka rowversion could just be considered the raw value captured from the server. Much faster.
After impelemtation of conversion to integer
CONVERT(BIGINT, [timestamp]) as Timestamp
I've got the result like
446701117
446701118
446701119
446701120
446701121
446701122
446701123
446701124
446701125
446701126
Yes, this is not a date and time, It's serial numbers
Why not try FROM_UNIXTIME(unix_timestamp, format)?
I had the same problem with timestamp eg:'29-JUL-20 04.46.42.000000000 PM'. I wanted to turn it into 'yyyy-MM-dd' format. The solution that finally works for me is
SELECT TO_CHAR(mytimestamp, 'YYYY-MM-DD') FROM mytable;
I will assume that you've done a data dump as insert statements, and you (or whoever Googles this) are attempting to figure out the date and time, or translate it for use elsewhere (eg: to convert to MySQL inserts). This is actually easy in any programming language.
Let's work with this:
CAST(0x0000A61300B1F1EB AS DateTime)
This Hex representation is actually two separate data elements... Date and Time. The first four bytes are date, the second four bytes are time.
The date is 0x0000A613
The time is 0x00B1F1EB
Convert both of the segments to integers using the programming language of your choice (it's a direct hex to integer conversion, which is supported in every modern programming language, so, I will not waste space with code that may or may not be the programming language you're working in).
The date of 0x0000A613 becomes 42515
The time of 0x00B1F1EB becomes 11661803
Now, what to do with those integers:
Date
Date is since 01/01/1900, and is represented as days. So, add 42,515 days to 01/01/1900, and your result is 05/27/2016.
Time
Time is a little more complex. Take that INT and do the following to get your time in microseconds since midnight (pseudocode):
TimeINT=Hex2Int(HexTime)
MicrosecondsTime = TimeINT*10000/3
From there, use your language's favorite function calls to translate microseconds (38872676666.7 µs in the example above) into time.
The result would be 10:47:52.677
Some of them actually does covert to a date-time from SQL Server 2008 onwards.
Try the following SQL query and you will see for yourself:
SELECT CAST (0x00009CEF00A25634 AS datetime)
The above will result in 2009-12-30 09:51:03:000 but I have encountered ones that actually don't map to a date-time.
Not sure if I'm missing something here but can't you just convert the timestamp like this:
CONVERT(VARCHAR,CAST(ZEIT AS DATETIME), 110)

Resources