Convert Time zonoffset value to varchar - sql-server

Query:
DECLARE #TimeZoneOffset datetimeoffset
SELECT #TimeZoneOffset = Time_Zone_Offset
FROM OFFSET_TABLE WHERE Active=1
Time_Zone_Offset column contains value like -6:00 (only offset)
When I do SELECT #TimeZoneOffset it throws me an error
Conversion failed when converting date and/or time from character string.
I know I am doing something wrong. I may need to CONVERT/CAST but can't get o/p so far.
Any help

To visualize what is happening here, try this:
DECLARE #x VARCHAR;
SET #x = 'abcdefghijklmnop';
SELECT #x;
Result:
----------
a
You have silently lost data from your variable, because you didn't bother declaring a length for your VARCHAR. In your case, I think you are ending up trying to use the string - somewhere, as opposed to the string -6:00.
I'm not sure how a simple SELECT yielded the error you mentioned; I suspect you are using it in some other context you haven't shown. But please try it again once your variable has been declared correctly.
Now I see why, your question wasn't correct - you said you were converting to VARCHAR but you weren't. This is not really unexpected, as -6:00 is not a valid DATETIMEOFFSET value; there is expected to be date and time components as well, otherwise the data type would just be called OFFSET. A valid DATETIMEOFFSET, according to the documentation, is:
DECLARE #d DATETIMEOFFSET = '1998-09-20 7:45:50.71345 -05:00';
So perhaps you have some datetime value and you want to apply the offset, well you can use SWITCHOFFSET() for that. However -6:00 is not a valid value; it needs to be in [+/-]hh:mm format (notice the leading 0 above, which seems to be missing from your sample data). So this would be valid:
DECLARE #datetime DATETIME = GETDATE(), #offset VARCHAR(6) = '-06:00';
SELECT SWITCHOFFSET(#datetime, #offset);
You need to correct the data in your offsets table and you need to change the way you are using the output. Personally, I've found it easier to stay away from DATETIMEOFFSET and SWITCHOFFSET(), especially since they are not DST-aware. I've had much better luck using a calendar table for offsets, storing the offsets in minutes, and using DATEADD to switch between time zones. YMMV.

Related

Conversion failed when converting date and/or time from character string during update only

I have been searching for a resolution for a long time now and just can't seem to formulate a query that brings back the resolution so as a last resort I have posted here.
I have a SQL server table with a varchar column that has the date and time stored in this format
"1/1/2013 11:38:31 PM Some other text"
I needed this date and time portion of this data to be stored in another column in datetime datatype. So I created a new column called DateTimeLog of type datetime.
I then used left to chop off the extra text and convert to change the value to datetime format and got the result I would expect.
select CONVERT(DATETIME,(rtrim(left(olddate, 21)))) from mytable
results:
"2013-01-01 23:38:31.000"
So far, so good. this is what I would expect. My troubles begin when I attempt to update my new datetime column with the results of this CONVERT statement.
update mytable
SET DateTimeLog = CONVERT(DATETIME,(rtrim(left(olddate, 21)))) from mytable
I get the infamous "Conversion failed when converting date and/or time from character string" error message.
Conversion failed when converting date and/or time from character string.
I have also attempted to use cast
update mytable
SET DateTimeLog = (cast(CONVERT(DATETIME,(rtrim(left(oldtable, 21)))) as datetime)) from mytable
the error persists. As best I can tell the convert is working correctly because I can see the result set from a select, but getting that result into a new column has eluded me thus far.
thanks,
Your string isn't going to consistently be 21 characters long. Your sample data shows a single character month and a single character date. What if it's, say, 12/13/2018?
That said, you need a more robust way to isolate that timestamp. I used a PATINDEX to capture the position of the last colon in the time component, with a couple of regexes in there to account for the numbers & the AM/PM thing. Then I added 6 to it to get to the end of the string of interest.
This seems to work:
DECLARE #t TABLE (olddate VARCHAR(100));
INSERT #t
(
olddate
)
VALUES
('12/13/2018 11:38:31 PM Some other text')
,('1/1/2018 11:38:31 PM Some other text');
SELECT CAST(LEFT(olddate,PATINDEX('%:[0-9][0-9] [AP]M%', olddate)+6) AS DATETIME)
FROM #t;
Results:
+-------------------------+
| 2018-12-13 23:38:31.000 |
| 2018-01-01 23:38:31.000 |
+-------------------------+
Rextester: https://rextester.com/BBPO51381 (although the date format's a little funky on the output).

Troubleshoot conversion failed when converting date and /or time from character string

I know this has been addressed 1000 times but something screwy is going on and I need help with ideas to troubleshoot.
Using MS SQL Server 2012
I have a date stored as INT in the YYYYMMDD format.
I need to turn that into a date and I always use this:
CONVERT(DATE,CONVERT(VARCHAR,YYYYMMDD),101)
*I know not specifying the length of the varchar is bad form but where I'm doing this has never caused an issue and just to be thorough I tried it anyway to no avail.
That conversion always works. Always, until today.
Today, this conversion doesn't work and I get the above mentioned error in title.
One thing I've done is run part of the query to look at the values to make sure I don't find something dumb like this in my values:
20170102
20170304
-->2017ABCD
20170704
What else can I do?
To expand on my initial comment
Example
Declare #YourTable table (SomeCol varchar(25))
Insert Into #YourTable values
('20170102'),
('20170304'),
('2017ABCD'),
('20170704')
Select SomeCol
,AsDate = try_convert(date,SomeCol)
From #YourTable
Returns
SomeCol AsDate
20170102 2017-01-02
20170304 2017-03-04
2017ABCD NULL
20170704 2017-07-04

"The conversion of a datetime2 data type to a smalldatetime data type resulted in an out-of-range value."

I have a stored procedure causing a date conversion error. I don't know what changed as it's been working fine for many months but our development group is downstream of a national DW. There are two variables used to retrieve date range #DatePlus1 & #DayOffset.
#DatePlus1 is used in a dozen or so CTEs that I won't post unless asked for but I commented out the date calculation and hard set the date as '2017-10-31 00:00:00' and ran the procedure. It ran without error but I don't understand why.
The original code set the #DatePlus1 as Date type which generated same error in subject.
SELECT #DayOffset = ISNULL(#DayOffset, 0)
DECLARE #DatePlus1 DATETIME2(0) --Was date but changed to datetime2 while troubleshooting
SELECT
#DatePlus1 = CONVERT(DATE, DATEADD(dd, 1, DATEADD(dd, #DayOffset, GETDATE())))
A static value would NOT be interpreted any differently than the same value passed in a variable. Therefore the problem is not what you think it is.
You are testing with a static variable that is in-range for a smalldatetime datatype. And your test is successful.
Therefore the reason your test fails when you use a variable is because the value of the variable is out-of-range for a smalldatetime, OR results in some selection of a value that is out-of-range.
The problem is not that you are using a variable. The problem is that you are giving that variable an invalid value in some part of the code that you are not showing in your question.
To troubleshoot this, you need to find all the places in your code where you populate a smalldatetime variable or column, and use PRINT or SELECT statements to see what value you are trying to insert into the smalldatetime.
So far I've done 2 and it fails on 1 RequestDate comparison but not another but RequestDate appears in different tables.
So another thing to look for is places in your code where the value of your variable could get changed programmatically. I would look right between the two queries where the comparison works, and the one where it fails.

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