Conversion of 12 Hour Clock to 24 Hour Clock in SQL Server - sql-server

SELECT CONVERT(time ,' 00:00:12 PM')
Why doesn't the above Code work. What to do to make it Work.
For some reason "00:00:00 PM" to "00:59:59 PM" fails to convert into Time :(
"1:00:00 PM" gets successfully Converted.
Thanks in Advance. Someone please help me out.

00:00 PM does not exist I think. It is either 12:00 AM or 12:00 PM. 00:00 only exists in the 24h format.

CONVERT can be used as a CAST but with a FORMAT for dates to get this already well understood literal to be a specific format.
You however want to use PARSE to get this maybe not so well understood literal to become a datetime or time (which doesn't have a format). To tell the engine how to interprrt the literal we type USING.
Check it:
SELECT PARSE(' 00:00:12 PM' AS time USING 'en-US')

Related

How to convert custom string date format to desired output SQL

In SQL server, I have a date string that looks something like 09/08/2021. Representing dd/MM/YYYY. So in this case its 09 August 2021. Every method I attempt converting this explicitly into a date format automatically converts this to 2021-09-08. So SQL incorrectly converts to 08 September 2021.
SELECT TRY_CAST(Convert(VARCHAR(15),'09/08/2021',103) as date);
SELECT TRY_CAST(Convert(VARCHAR(15),'09/08/2021',111) as date);
Both incorrectly give:
2021-09-08
Desired result is:
2021-08-09
I've tried the various different versions of the above but keep getting the incorrect conversion. I don't really want to go down the road of changing the source data either.
I am amazed no similar question has been previously asked regarding this.
How can this be converted explicitly using functions in SQL Server?
As per the comments, it was the conversion that was the mistake. Adjusted to
TRY_CONVERT(DATE, '09/08/2021', 103)
This fixes the issue.

Group by 24hr Period starting at 7:00 AM

I have some high frequency data that I need to group by 24hr period starting at 7:00 AM. How can I do that?
For example, there is a timestamp every 5 minutes and I want to group it by day, but each day starts at 7:00 AM.
How would I go about doing that?
SQL Server 2016.
I am going to presume your time is probably in UTC.
I would look at converting the time to the timezone you need. Then it gives you more options of how you want to deal with the data, also you could customize the report for people in different zones. For some info about timezones I found this stackoverflow post.
Convert datetime value from one timezone to UTC timezone using sql query
Something like this in an group by clause should work. The code below says if the hour is under 8 then it's still yesterday, else it's really today. You would need to replace the getdate() with the correct column. If I Am correct about that the times are in UTC/GMT I would recommend looking at just converting it to the timezone you want.
case when datepart(hh, getdate())<8 then
cast((DATEADD(day, -1, getdate())) as date)
else
cast(getdate() as date)
end

Date conversion failing on selected date

Hi, I am wondering why this date: 11/4/2011 fails when I do a conversion.
E.g. convert(date,date_field,103) returns as expected but when the convert function is trying to convert this date 11/17/2011 it returns conversion failed.
I tried ltrim to see if there is white space but the same error is returning,
I also tried isdate to make sure it is a date and isdate is returning greater than 0.
I know this is basic but could someone give an insight as to why it is rejecting the date?
Thank you.
MSDN says 103 = dd/mm/yyyy. Your date is "11/17/2011".
Is there a 17th month?
use varchar instead of date. like below
Select Convert(Varchar,'11/17/2011',103)

Datetime field using military time - need time only in standard time

I have a datetime field in SQL Server 2008 which stores the date and time in military format (or international format if you prefer)
examples:
2011-02-15 10:00:00.000
2011-02-15 15:30:00.000
2011-02-15 17:30:00.000
I need to retrieve the time only portion of this in standard U.S. time format.
So
2011-02-15 10:00:00.000 needs to become 10:00 AM
2011-02-15 15:30:00.000 needs to become 3:30 PM
2011-02-15 17:30:00.000 needs to become 5:30 PM
I am looking for the best way to accomplish this in T-SQL please.
One way is:
Convert to varchar, which will give you:
Select CONVERT(varchar, #dt, 100) -- where #dt is your date time
Feb 15 2011 3:30PM
Subsequently, to remove the date, and get 7 chars from the right, which is the most, and TRIM is added just for extra safety, but probably isn't needed.
Select LTRIM(RIGHT(CONVERT(varchar, #dt, 100),7))
Will give you 3:30PM
Side Note: Denali makes this easier, no more magic numbers
As you requested this in T-SQL, you might want to look at the CAST and CONVERT syntax which specifically lists various date and time formats.
For example:
select convert(varchar, getdate(), 100)
Would give you:
Feb 3 2012 3:26PM
DateTime is stored in an internal representation - there is no format associated. When you need to display the DateTime, specify the formatting you want for converting the DateTime into a string.
It is much better to let the application decide on formatting than formatting in SQL.
See standard and custom Date and Time Format Strings for more information.

how to interpret this timestamp?

Lets say this is the time-stamp: 2011-07-06T00:00:35.851-07:00
What does that tell me? This is how I am trying to understand it:
2011-07-06 - date
00:00:35 - hh:mm:ss
851 - micro seconds??
07:00 - what does that tell me?
I need to convert this to UTC if possible with C.
Edit 0: Thanks for the responses by #RichieHindle and #Marc B. I now understand the GMT offset.
My problem now is, I am not getting correct value out of getdate.
It says it's July 6th, 2011, 35.851 seconds past midnight, in the GMT-7 time zone. To convert to UTC (GMT-0 timezone), you'd need to add 7 hours (-7 + 7 = 0), making it 2011-07-06T07:00:35.851-00:00
851 is milliseconds (thousandths of a second) and -07:00 is the timezone (UTC minus 7 hours).

Resources