Time format hhmm to hh:mm sql server 2005 - sql-server-2005

I want to find the time difference so i am trying to use DATEDIFF option.
But the problem is i am having the time values in hhmm format(eg: 1715). So it showing out of range error. so i want to convert it to hh:mm format. How to convert hhmm to hh:mm format?

Have you tried something like
DECLARE #TimeVal VARCHAR(4)
SELECT #TimeVal = '1715'
select CONVERT(DATETIME,LEFT(#TimeVal,2) + ':' + RIGHT(#TimeVal,2),8)
Output
1900-01-01 17:15:00.000

Related

What is the difference between hh and HH in dateformatting in SQL?

When formatting my time i get different results depending on the use of hh or HH.
Can someone tell me why this happens?
The problem occurs with the following code, returning a NULL on the 2nd result.
declare #TIMEPART time = getdate()
declare #datetime datetime2(7) = getdate()
select format(#timepart, 'hh\:mm'), format(#timepart, 'HH\:mm'), format(#datetime, 'hh\:mm'), format(#datetime, 'HH\:mm')
The HH:mm format is not a valid conversion for the time datatype and for the format(time, time format) function it is not the correct syntax. Because of this the format function returns a null. HH:mm is the conversion type for 24 hour clock for datetime datatypes but is not compatible with the time datatype as the time datatype is already in the 24 hour clock format(not sure why this is the case but I think it is because the time datatype can do more than just represent 24h clock time). The AM\PM format type hh:mm is only valid with the datetime datatypes and for time it will just return the 24 hour time since this is all a time datatype can return. You would have to convert the time to a datetime if the input value datatype is time in order to display the AM/PM format as I have shown below.
declare #TIMEPART time = '21:20:20.0570000'
declare #datetime datetime2(7) = '21:20:20.0570000'
select format(#timepart, 'hh\:mm'), format(convert(datetime,#timepart), 'hh:mm'), format(#datetime, 'hh\:mm'), format(#datetime, 'HH\:mm')
If you still want the data type of the second column to be time then you would have to convert back to time
cast(format(convert(datetime,#timepart), 'hh:mm') as time)
However in this example the time would be 9:20 AM and not 9:20 PM since time is only stored as a 0 to 23 value for the hour. This is a good example of why is is advisable to always try to return any time value in the 0-23 hour format. If you do need to display the time in the 12 hour format you are better off converting it to a varchar.
convert(varchar(15), format(convert(datetime,#timepart), 'hh:mm'))
One other thing to note is that for datetimes the \ in the format is not needed, for example
format(#datetime, 'hh\:mm')
and
format(#datetime, 'hh:mm')
will return the same value. However the \ is needed for time datatype functions.
format(#timepart, 'hh\:mm')
will return a hh:mm time but
format(#timepart, 'hh:mm')
will return null

SQL Column Numeric to DATE

I am trying to convert numeric column (with 13 digits) to DATE and all i can do is this so i can see the date as a string.
CONVERT(VARCHAR(10),(DATEADD(SECOND, Start_Date/1000 ,'1/1/1970')),104)
What can i do so this will end up like DATE so i can filter it later?
The database is MS SQL.
Thanks in advance!
This expression should convert it to a datetime:
DATEADD(SECOND, Start_Date/1000 , '1970-01-01)
If you want a date, just convert this to a date:
CAST(DATEADD(SECOND, Start_Date/1000 , '1970-01-01') as DATE)
Note: I changed the date format to the ISO standard YYYY-MM-DD format.

converting to date time with only date and hour passed in

I am using SQL server, i want to insert datetime into my datetime column. The parameters i recieved as follows
#hour int,
#date varchar(10) -- format YYYY-MM-DD
How do I convert to this datetime format:
YYYY-MM-DD HH:MM:SS
MM will be 00 as we only consider the hour.
HH:MM will be in 24 hours format
Example :
#date = '2014-10-2'
#hour = '8'
Should be converted to
2014-10-2 08:00:00
How do I do this?
I consider that both the variables are of varchar type since you enclosed both the variables with quotes
SELECT CONVERT(DATETIME, #date + ' ' + convert(varchar(10),#hour) + ':00:00')
here is one another way to do this:-
declare #hour int=8,
#date varchar(10)='2014-10-2'
select dateadd(hh,#hour,CONVERT(datetime,#date))

Convert UTC Seconds to String in SQL

I have a column with UTC Seconds in SQL.
How can I convert UTCSeconds to varchar in order to get this output in a select statement:
DD-MMM-YYYY HH:mm:ss
Example: 1400249277 in table
16-May-2014 15:07:57 as output of Select statement
I don't have a SQLServer available right now but this looks like it might be what you're looking for : DATEADD. You are probably considering the number of seconds since January the 1st 1970, so it would be something alike :
SELECT DATEADD (second, <your number of seconds>, '1970-01-01')

SQL Server Convert ISO 8601 not working as documented

Per MSDN convert should properly parse ISO 8601 dates with timezone using 127 as the style parameter.
The optional time zone indicator, Z, is used to make it easier to map XML datetime values that have time zone information to SQL Server datetime values that have no time zone. Z is the indicator for time zone UTC-0. Other time zones are indicated with HH:MM offset in the + or - direction. For example: 2006-12-12T23:45:12-08:00.
All of the following are valid ISO 8601 dates but return Conversion failed when converting date and/or time from character string.
select convert(datetime, N'2014-02-07T13:51:00+07:00', 127)
select convert(datetime, N'2014-02-07T13:51:00+07', 127)
select convert(datetime, N'2006-12-12T23:45:12-08:00', 127)
Anyone have a solution or workaround for this issue?
Workaround?: Use datetimeoffset:
select convert(datetimeoffset, N'2014-02-07T13:51:00+07:00', 127) --<-- This one works...
select convert(datetimeoffset, N'2014-02-07T13:51:00+07', 127)
select convert(datetimeoffset, N'2006-12-12T23:45:12-08:00') --<-- and this one works...
use datetimeoffset or datetime2 instead of datetime