Converting datetime not converting datetime - sql

So I have a table and I am trying to convert the time from its current format that looks like this
Sep 17 2020 1:07AM
to
2020-09-16 20:07:00.000
if I try to update the entire table by
UPDATE
OOTYPE
SET
OOTYPE.LastModified = CONVERT(DATETIME, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, LastModified),
DATENAME(TzOffset, SYSDATETIMEOFFSET())))
it runs successfully but the times are still in the format
Sep 17 2020 1:07AM
However, if I just run a simple select statement to show it changed
SELECT CONVERT(DATETIME, SWITCHOFFSET(CONVERT(DATETIMEOFFSET,lastmodified), DATENAME(TzOffset, SYSDATETIMEOFFSET()))) FROM [OOTYPE]
The output is exactly what I'm looking for.....
2020-09-16 20:07:00.000
Anyone have any idea where I'm screwing this simple change up at, I have put an embarrassing amount of time into this....

If I follow you correctly:
SELECT CONVERT(varchar, CONVERT(datetime, 'Sep 17 2020 1:07AM'), 121)
You should use convert style 121 which is yyyy-mm-dd hh:mi:ss.mmm.

Related

Conversion of datetime to nvarchar(8) in T-SQL

I'm trying to convert a datetime column to ISO format, as yyyymmdd. For example, I would like to be able to convert '13 dec 2018' to '20181213'.
According to Microsoft's T-SQL Docs, I should be able to do this using
convert(nvarchar(8), '13 dec 2018', 112)
however this doesn't work - I get the result '13 dec 2', which looks to be nothing more than the original string cut down to 8 characters.
I have also tried using
convert(datetime, '13 dec 2018', 112)
which gives me the result of 'Dec 13 2018 12:00AM' - again, nothing like what the function is supposed to produce.
What I am doing wrong? I could solve the problem easily enough using datepart() and concatenated strings, but I'd rather use the more elegant approach if possible.
Combine them:
convert(nvarchar(8), convert(datetime, '13 dec 2018'), 112)
I don't recommend using the format 112 for the first conversion, because it is misleading. SQL Server is very good at converting without a format. If you do use one the appropriate one is 106.
I figured it out - because I was entering the date as a string, and not converting it to a datetime value first, it was treating my date as if it were a string. When I handle it this way:
convert(nvarchar(8), cast('13 dec 2018' as datetime), 112)
I get the expected result, '20181213'.
select convert(nvarchar(8), cast(GETDATE() as datetime), 112) as TodayDate
YYYYMMDD format can obtain by datetime to varchar format.
select convert(varchar(8),convert(datetime, '13 dec 2018'),112)
The above query provides the required result

t-sql convert datetime to time only with NO SECONDS

There are many t-sql CONVERT strings to produce quite a variety of date and or time strings. But I cannot find the solution to needing no date and no seconds.
We want to return the time only from a datetime field, and eliminate the seconds. It would not matter if the seconds were truncated or rounded, but we need to show no seconds.
desired results- from any DATETIME field
10:00 AM
11:00 AM
4:59 PM
any and all insights or suggestions appreciated!!
Would this do it?
select CONVERT(varchar(15),CAST(GETDATE() AS TIME),100)
Just change out GETDATE() with your date variable.
Try this:
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
Put your DATETIME field instead GETDATE()
If you want a space then the AM /PM try this:
SELECT
Left(CONVERT(varchar(15),CAST(GETDATE() AS TIME),100),(len(CONVERT(varchar(15),CAST(GETDATE() AS TIME),100))-2)) +' ' + right((CONVERT(varchar(15),CAST(GETDATE() AS TIME),100)),2) as NiceTimeAMPm
NiceTime
7:35 AM
3:00 PM

How to assign current date with specific time to column?

How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/

How to extract date from datetime stored as a varchar

How can I convert the string 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', stored as a varchar(max), to the datetime value '09-03-2012', which is the date part of the string?
Use convert and reference to this page http://msdn.microsoft.com/en-us/library/ms187928.aspx
e.g.
CONVERT(varchar(8), GETDATE(), 112)
According to MS: http://msdn.microsoft.com/en-us/library/ms187928.aspx
The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
and the table lists 105 as the code for the style you want.
So, for your example date:
CONVERT(VARCHAR(10), 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', 105)
should probably do the trick.
I had to chop up your date first, but here goes:
DECLARE #ds varchar(30)='Fri, 9 Mar 2012 10:43:21 +0000 (UTC)';
SELECT CONVERT(VARCHAR(10)
,CAST(SUBSTRING(#ds
,CHARINDEX(' ',#ds)
,LEN(#ds)-(PATINDEX('%[0-9][0-9][0-9][0-9]%',#ds)+6))
AS DATETIME)
,105
)
Result:
09-03-2012
If you have SQL Server 2012, you can try this:
select CONVERT(date, TRY_PARSE('Fri, 9 Mar 2012 10:43:21 +0000' AS datetime2))
NOTE: (UTC) is removed, it didn't parse correctly.
Also, be aware if you need to localize that time value. For example, I live in AZ, so typically I just subtract -7 hrs from UTC times to localize. You will need to make similar adjustments based on the time zone

sql server: what's wrong with my date data?

i have a column with dates, but it is a varchar:
8/31/2010 9:48
8/31/2010 9:49
8/31/2010 9:51
8/31/2010 9:52
8/31/2010 9:55
8/31/2010 9:59
8/31/2010 10:11
8/31/2010 10:13
8/31/2010 10:16
8/31/2010 10:37
8/31/2010 10:42
i made sure that none of these will be a BAD date:
SELECT *
FROM qcvalues.dbo.batchinfo
WHERE ISDATE(reporttime) <> 1
this returned 0 results
question:
i need to return dates between a certain range:
select rowid from qcvalues.dbo.batchinfo where CONVERT(DATE, Substring( reporttime, 1, LEN(reporttime)), 103)
between cast('2010-08-01' as datetime) and CAST('2010-08-31' as datetime)
and i am getting this error;
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
what is wrong with my conversion?
If you need to store dates then use a datetime column in the future
does this work?
WHERE CONVERT(DATE,RTRIM(reporttime))
BETWEEN '2010-08-01' and '2010-08-31'
If not use SET DATEFORMAT MDY before running it
And if you have to store it in a varchar column then use YYYYMMDD format...that way you can do
WHERE reporttime like '201008%' if you want August 2010
This will solve your problem:
select rowid
from qcvalues.dbo.batchinfo
where
CONVERT(DATE, reporttime, 101) >= '20100801'
-- style 101, not 103
-- also notice date conversion invariant format YYYYMMDD with no separators
AND CONVERT(DATE, reporttime, 101) < '20100901'
-- using BETWEEN with an end date of '8/31/2010' will skip
-- times between '8/31/2010 00:00:00.003' and '8/31/2010 23:59:59.997'
Try this to see what the problem is:
select convert(datetime, '8/31/2010 9:48', 103)
select convert(datetime, '8/31/2010 9:48', 101)
put SET DATEFORMAT MDY before your query.
This will strip out the time portion too
select rowid
from qcvalues.dbo.batchinfo
Where cast(floor(cast(cast(reportTime as datetime)as float))as datetime)
between cast('2010-08-01' as datetime)
and cast('2010-08-31' as datetime)
Remember, this CAST('2010-08-31' as datetime) will have its time portion as 00:00.
Consider casting your varchar data as smalldatetime, and being specific about the boundaries of times in your between. No need to be converting, substring, etc. Just one CAST will do.
Consider this as a potential solution:
SELECT rowid from qcvalues.dbo.batchinfo
WHERE CAST(reporttime as smalldatetime)
BETWEEN '2010-08-01' AND '2010-08-31 23:59'