TSQL Convert VARCHAR of HH:MM:SS.mmm to TIME - sql

Unable to convert 15600.02 value to time .
Ex :
select cast(stuff(stuff('15600.02',5,0,':'),3,0,':') as time)
ERROR :
Conversion failed when converting date and/or time from character string.
can any one help on it .
Thanks,
Kumar

Assuming this is really what this number represents...
The location you are using in the STUFF command is 1 off when earlier than 10 o'clock.
This isn't pretty, but try select cast(stuff(stuff(REPLACE(SPACE(7 - CHARINDEX('.', '15600.02' + '.')),' ','0') + '15600.02',5,0,':'),3,0,':') as time)

Related

SQL Convert & Cast Nvarchar Time to a decimal

I'm working on a legacy database and need to parse info from one database to another, parsing it into the new database is easy enough but first I need to create the query to convert and cast the following in the legacy SQL Server database:
WorkedHours(NVARCHAR(10)) is in text format 07:30
I need to convert and cast this as a decimal ie 7.5
I have searched around for the answer to this but can not find anything that has worked, so thought I would put it out there to see if any of you has any ideas.
Edit - What I should of asked is. What is causing an error converting to an int from a character with a value of 0 when trying to trying to convert and cast a time to a decimal?
DATEDIFF(
MINUTE,
0,
CAST('07:30' AS TIME)
)
/
60.0
Works up to '23:59' only
EDIT:
Based on a comment elsewhere, you have some 'bad' values.
This may find them...
SELECT
*
FROM
yourTable
WHERE
TRY_CONVERT(TIME, worked_hours) IS NULL
And as such, this is a safer version of my expression....
DATEDIFF(
MINUTE,
0,
TRY_CONVERT(TIME, worked_hours)
)
/
60.0
(Returns NULL for values that failed to parse.)
There's no reason to pull out the date/time types. Just do some simple string parsing:
cast(left(right('0' + WorkedHours, 5), 2) as int)
+ cast(right(WorkedHours, 2) as int) / 60.00
This won't have any limitations on 24 hours or anything like that. It just assumes that you've got one or two digits before a colon and two digits after.
This should work in SQL Server and an example-string "1101:56" (1101h & 56 minutes) | in general from 0h to >24h:
-- Take all hours before ":" and all Minutes (2 digits) after ":" and convert it to decimal.
select convert(decimal,left('1101:56',CHARINDEX(':','1101:56')-1)) + ( convert(decimal,right('1101:56',2))/60 );
-- with column-placeholder "time_str_from_table"
select convert(decimal,left(time_str_from_table,CHARINDEX(':',time_str_from_table)-1)) + ( convert(decimal,right(time_str_from_table,2))/60 );
If the source table have NULL-Values, than use "ISNULL" with substitution-value "0.0":
-- with column-placeholder "time_str_from_table"
select isnull( ( convert(decimal,left(time_str_from_table,CHARINDEX(':',time_str_from_table)-1)) + ( convert(decimal,right(time_str_from_table,2))/60) ), 0.0);

how to convert the 24 hr to 12 hr in sql

I have column of 24 hr and i need to change it to 12 hr, Please help .
Start time
174300
035800
023100
The result should be
Start time
05.43 PM
03.58 AM
02.31 AM
Use STUFF function to convert string to Time format
SELECT CONVERT(VARCHAR,CAST(STUFF(STUFF(ColumnName,3,0,':'),6,0,':') AS TIME),100)
Using one of the examples above - the following will work.
You need to split the data into hours/minutes and cast it to time format, than convert it to the relevant type:
declare #data int
set #data = 174300
select convert(VARCHAR(15),cast(cast(left(#data, 2 )as varchar(2)) + ':' + cast(substring(cast(#data as nvarchar(6)), 3,2 )as varchar(2) ) as time),100)
Don't store time as varchar, instead alter your table and change the column type to datetime.
SELECT right(convert(varchar(25), Start Time, 100), 7)
The 100 you see in the function specifies the date format mon dd yyyy hh:miAM (or PM), and from there we just grab the right characters.
You can see more about converting datetimes here.

SQL- How to exclude wrong time(saved in form of nvarchar) in my dataset?

I am trying to convert records in form of nvarchar into time. For example:
1520 into 15:20:0000
But it throws error: Conversion failed when converting date and/or time from character string.
Because there are wrong timestamps in the dataset like values:
4059','7054','4054','4030','3040','6046','7555','8030', '1169'
I am using the query:
select [QHHHMM],
cast(SUBSTRING([QHHHMM], 1, 2) + ':' + SUBSTRING([QHHHMM], 3, 2) as time) as TimeHistory
FROM [MOCK124].[dbo].[F2QH]
where
[QHHHMM] not in ('4059','7054','4054','4030','3040','6046','7555','8030')
and [QHHHMM] not in ('1169')
But I still see there are few more records that are invalid as time. Could anyone please assist what should be the where clause to exclude all the invalid timestamps?
Thanks
Look at the hours/mins as integers?
where cast(left(QHHHMM, 2) as int) between 0 and 23
and cast(right(QHHHMM, 2) as int) between 0 and 59
Figured out, we can use
where
[QHHHMM] > '2359'
or
substring([QHHHMM], 3,2) > '59'
Thanks!

Convert a value of 8:00 to 8.00

I want to convert a value of 8:00 to either a 8 OR a 8.00 so then I can multiply this number to another INT value such as 2.
Right now I get a error message saying:
Conversion failed when converting the nvarchar value '8:00' to data type int.
I have tried CAST(MYCOLUMN AS INT) but it did not work.
Can you kindly help?
Best Regards,
I'm still not sure what you're doing, however it is it actually a time and you want they hour you can do this.
SELECT DATEPART(HOUR, CAST('8:00' AS TIME));
EDIT:
SQL 2005 doesn't have the TIME data type so you'd have to do this.
SELECT DATEPART(HOUR, CAST('1900-01-01 ' + '8:00' AS DATETIME));
How about:
CAST(replace(MYCOLUMN, ':', '.') AS float)
(This assumes that your internationalization settings interpret . as a decimal place.)
or
CAST(left(mycolumn, charindex(':', mycolumn) - 1) as int)

SQL Server: How to concatenate string constant with date?

select packageid,status+' Date : '+UpdatedOn from [Shipment_Package]
The below error is appeared when executing the above code in sql server. The type of UpdatedOn is DateTime and status is a varchar. We wanted to concatenate the status, Date and UpdatedOn.
error:
Conversion failed when converting date and/or time from character
string.
You need to convert UpdatedOn to varchar something like this:
select packageid, status + ' Date : ' + CAST(UpdatedOn AS VARCHAR(10))
from [Shipment_Package];
You might also need to use CONVERT if you want to format the datetime in a specific format.
To achive what you need, you would need to CAST the Date?
Example would be;
Your current, incorrect code:
select packageid,status+' Date : '+UpdatedOn from [Shipment_Package]
Suggested solution:
select packageid,status + ' Date : ' + CAST(UpdatedOn AS VARCHAR(20))
from [Shipment_Package]
MSDN article for CAST / CONVERT
Hope this helps.
To account for null values, try this:
select packageid,
'Status: ' + isnull(status, '(null)') + ', Date : '
+ case when UpdatedOn is null then '(null)' else convert(varchar(20), UpdatedOn, 104) end as status_text
from [Shipment_Package]
Convert Datetime column into varchar for sql Server you can use below code
select packageid,status+' Date : '+CONVERT(VARCHAR,UpdatedOn,120) from [Shipment_Package]
120 code will convert your date into this format : 2020-06-12 15:09:00
other format you can use from this link