Convert Varchar to Time
I need my code to add the varchar CPUTM field + varchar CPUZIPTIM field which both has time values to see if greater than 2 hours. How do I do this when both fields are varchar. The value in CPUTM field is
335:55:20.97 duration time. My code is below.
CPUTM = 335:55:20.97 duration time
CPUZIPTM = 0:00:01.96
duration time
select * FROM [SMF_DATA].[dbo].[System_Management_Facility]
WHERE ((convert(varchar(13), CONVERT(time, CPUTM) + CONVERT(time, CPUZIPTM))) > '02:00:00.00')
Well, there is no time span in T-SQL, so you need either to store it in the smallest necessary unit (seconds, milliseconds) or to convert ít on the fly.
E.g.
DECLARE @CPUTM VARCHAR(255) = '335:55:20.97';
DECLARE @CPUZIPTM VARCHAR(255) = '00:00:01.96';
DECLARE @T BIGINT = PARSENAME(REPLACE(@CPUTM, ':', '.'), 1) +
PARSENAME(REPLACE(@CPUTM, ':', '.'), 2) * 100 +
PARSENAME(REPLACE(@CPUTM, ':', '.'), 3) * 6000 +
PARSENAME(REPLACE(@CPUTM, ':', '.'), 4) * 360000 +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 1) +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 2) * 100 +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 3) * 6000 +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 4) * 360000;
SELECT @T / 360000,
(@T % 360000) / 6000,
(@T % 6000) / 100,
(@T % 100);
Similar Messages
-
How to convert varchar to date datatype while insert or update in table
Hai All
I need to convert to varchar to date.
I have two Tables T1,T2
T1 Structure
Code varchar
Time varchar
Date varchar
T2 Structure
Empname var
Empcode var
Intime date
Outtime date
Intrin date
Introut date
Att_date
Now i need to move Time form T1 to T2 Intime,outtime,intrin,introut according some condition
So now i need to convert Varchar to Date while insert or update
I have tried something
Insert into T1 (code,intime,att_date)values
(code,To_date(Date||time,'dd-mon-yyyy hh24mi'),att_date);
OR While update
Update T2 set Outtime=To_date(Date||time,'dd-mon-yyyy hh24mi') where...
I got an error Ora-01861
Regards
Srikkanth.MYou didn't show any example of your date or time values, butyou might need to add a space between them, like
To_date(Date || ' ' || time,'dd-mon-yyyy hh24mi') -
How to write a query to get the time difference of two varchar type time columns
Hi,
I want to get the time difference between the two varchar type columns.please see the attached image for more details:
My requirement is like:
timestarted
timeended
timediff
9:00:00
10:00:00
1:00
9:15
9:30:00
0:15Storing time alone as VARCHAR2 value is a incorrect design. Always store it as DATE or TIMESTAMP.
If you already have a messed up design and cant change it, then you need to convert your VARCHAR2 time into a DATE or TIMESTAMP and find the difference. I have converted it to TIMESTAMP and obtained the difference as INTERVAL.
SQL> with t
2 as
3 (
4 select '09:00:00' timestarted, '10:00:00' timeended from dual
5 union all
6 select '09:15:00' timestarted, '09:30:00' timeended from dual
7 )
8 select timestarted
9 , timeended
10 , (timeended - timestarted) day to second diff
11 from (
12 select to_timestamp('01011900' || timeended, 'ddmmyyyyhh24:mi:ss') timeended
13 , to_timestamp('01011900' || timestarted, 'ddmmyyyyhh24:mi:ss') timestarted
14 from t
15 );
TIMESTARTED TIMEENDED DIFF
01-JAN-00 09.00.00.000000000 AM 01-JAN-00 10.00.00.000000000 AM +00 01:00:00.000000
01-JAN-00 09.15.00.000000000 AM 01-JAN-00 09.30.00.000000000 AM +00 00:15:00.000000
SQL> -
Hi All,
I have a requirement in my project like to convert the date time from one format to another.my situation is like to convert the date time from MM-DD-YYYThh:mm:ss to YYYY-MM-DDThh:mm:ss format. I am using the soa suite 11.1.1.6.
Can any one suggest me how to convert in the BPEL transformation.
Thanks,
Sanju.Hi Sanju,
Store the date to be converted into a variable viz. dateVar. Now, process an expression in assign as: xp20:format-dateTime($dateVar,'[Y0001]-[M01]-[D01] [h]:[m01]:[s01]').
Regards -
Convert Epoch Date Time to Date Time format
I am trying to convert Epoch Date time in GMT to Human Readable Date time format in UTC. I used the one below but it returns just the date in GMT but how can get datetime in UTC ?
select To_Char( To_Date( '01.01.1970 06:00:00','DD.MM.YYYY HH24:Mi:Ss') + STARTIME / 86400,'DD.MM.YYYY HH24:Mi:ss') FROM HQ_AVAIL_DATA_RLE;
I refered a couple of forums and applied everything, but still I wouldn't get the timestamp.
Please note that i'm looking for datetime stamp, not just the date.
Thanks in advance!
Edited by: 830754 on Jan 27, 2011 11:18 AM
Edited by: 830754 on Jan 27, 2011 11:19 AMWorks for me. I see date and time (submitted 1000 for STARTTIME):
select To_Char( To_Date( '01.01.1970 06:00:00','DD.MM.YYYY HH24:Mi:Ss') + 1000 / 86400,'DD.MM.YYYY HH24:Mi:ss')
from dual
TO_CHAR(TO_DATE('01
01.01.1970 06:16:40Or do you mean something else? -
How to convert 12 hour time format to 24 hour time format ?
Hi,
How to convert 12 hour time format to 24 hour time format is there any FM if not, please suggest me how to convert .
regards,
rakeshHi,
Have you tried function module HRVE_CONVERT_TIME
Input parameters will be like
TYPE_TIME B
INPUT_TIME 01:00:00
INPUT_AM_PM PM
Output
OUTPUT_TIME 13:00:00
Regards -
Function module for converting German Data/Time to Indian Date/Time
Dear All,
My requirement is to convert German date and time to Indian date and time. Is there any function module available or any way we can get it?
Thanks and regards,
Atanuhi,
first get the timezone of that country
CALL FUNCTION 'TZON_LOCATION_TIMEZONE'
EXPORTING
if_country = p_it_userid-zcountry
IMPORTING
ef_timezone = l_tzone
EXCEPTIONS
no_timezone_found = 1
OTHERS = 2.
then use the below statements
-- Convert from server time to GMT
CLEAR l_tstmp.
CONVERT DATE l_datum
TIME l_ctime
INTO TIME STAMP l_tstmp TIME ZONE sy-zonlo.
*-- Convert from GMT to user backup time
CONVERT TIME STAMP l_tstmp TIME ZONE it_usr_timezone-ztzone
INTO DATE it_usr_timezone-zdate
TIME it_usr_timezone-ztime. -
Convert varchar(20) date to date that looks like mm/dd/yyyy
Good afternoon,
I am a total novice at SQL and have been tasked to change a varchar(20) date column (shows up as m/dd/yy) to show up as mm/dd/yyyy. I've looked at the previous posts, but being a novice, it's difficult to wade through the non-applicable stuff. The column
that needs to be changed is field2, but I'm not sure how to construct a statement to reflect my objective.
Thank you in advance.The sample uses a table variable. You can program it the same way for a table:
DECLARE @t TABLE (dt VARCHAR(20))
INSERT INTO @t
VALUES ('1/2/12')
,('3/10/10')
,('7/23/9')
,('aa')
,('12/31/99')
,('10/20/00')
,('10/20/01')
SET DATEFORMAT mdy
SELECT dt
,CONVERT(VARCHAR(10), CASE
WHEN isdate(dt) = 0
THEN NULL
ELSE cast(dt AS DATE)
END, 101) AS newDt
FROM @t;/******** TRY_CONVERT() method ************/
SELECT dt, newDt=CONVERT(varchar(10),TRY_CONVERT(date,dt),101) FROM @t;
GO
dt newDt
1/2/12 01/02/2012
3/10/10 03/10/2010
7/23/9 07/23/2009
aa NULL
12/31/99 12/31/1999
10/20/00 10/20/2000
10/20/01 10/20/2001
dt newDt
1/2/12 01/02/2012
3/10/10 03/10/2010
7/23/9 07/23/2009
aa NULL
12/31/99 12/31/1999
10/20/00 10/20/2000
10/20/01 10/20/2001
Starting with SQL Server 2012 you can use TRY_CONVERT().
You can start studying datetime functions here:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Kalman Toth Database & OLAP Architect
T-SQL Scripts at sqlusa.com
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012 -
Convert varchar(255) to varchar(50)
I want to transfer data from a staging table to another table. The staging table has columns with datatypes I don't always want, and I'm converting them like so:
INSERT INTO [dbo].[my_table]
SELECT CONVERT(datetime,TimeIndex,103),
CONVERT(decimal(6,3),Network_In,3),
CONVERT(decimal(6,3),Network_Out,3),
CONVERT(decimal(6,3),Network_Total,3)
FROM [dbo].[staging_table]
How would I convert varchar(255) to varchar(50)?
Thanks in advance
AdamUsing substring is returning the column so thanks but now I can't convert datetime for some reason:
INSERT INTO [dbo].[mytable]
SELECT SUBSTRING(ClusterName,1,50) AS 'ClusterName',
CONVERT(datetime,TimeIndex,103),
ClusterID,
CONVERT(decimal(3,2),Memory,3),
CONVERT(decimal(3,2),CPU,3),
CONVERT(int,CPUTotal),
CONVERT(int,MemoryTotal)
FROM [dbo].[stagingtable]
GO
Now I get an error I didn't get before:
Implicit conversion from data type datetime to decimal is not allowed. Use the CONVERT function to run this query.
But if I run the datetime convert on it's own as a single column select then it works okay. Any ideas where I've gone wrong there?
To sum up - the above fails but the below works:
SELECT CONVERT(datetime,TimeIndex,103) AS 'TimeIndex'
FROM [dbo].[stagingtable]
Thanks
Adam -
Replace and convert varchar to decimal
Hi all
What could be the easiest way to replace and convert varchar(50) to decimal
My string '21 807,00' Need to have it in db as 21807.00
Thanks
AKEOn any version you can use somple convert with replace:
declare @MyStr varchar(10) = '21 807,00'
SELECT CONVERT(decimal(10,2), REPLACE(REPLACE(@MyStr, ',','.'),' ',''))
but if there is a different format and the convert will not work, then you will get error. Therefore it is best (as mentioned) to use TRY_CONVERT on newer server or use Try block on old servers
[Personal Site] [Blog] [Facebook] -
Converting varchar to datetime 103
Hi Guys
Im making a select query where in a colomn the date is yyyy-mm-dd, i need dd-mm-yyyy (103). A simple convert query does not work because the colomn is not set as a datetime but as a varchar. Its not possible to change the tables at the source.
The colomn name is DATE_IN
I think the following post could be helpfull but i dont know how to fix the given statements into the select query
http://stackoverflow.com/questions/1509977/convert-varchar-into-datetime-in-sql-server> in a colomn the date is yyyy-mm-dd, i need dd-mm-yyyy (103)
As suggested above, this is string date format to string date format conversion, purely string operations.
If you convert to DATE / DATETIME first (universal internal binary representation), then it may fail if invalid date.
You can use the ISDATE or TRY_CONVERT functions to check if the string date is valid.
DATETIME functions:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Kalman Toth Database & OLAP Architect
SQL Server 2014 Design & Programming
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012 -
How to convert varchar to int in MSSQL?
Hi,
I am using mssql. how can i convert varchar to int type.
for eg.
id (int) txt(varchar)
1 ---------10
2 ---------10a
3 ---------10b
i want to convert varchar to int. HOw? pls explain with query.
Thanks
edward"10a" wouldn't normally be considered a number. So what sort of number do you expect it to be?
Other than that you can look at the substring and convert/cast functions. -
Convert varchar to decimal - arithmetic overflow
I'm using SQL Server 2014 and I'm trying to convert data from a staging table over to a production table. I seem to be getting an Arithmetic overflow error converting varchar to numeric on the decimal conversion. I'm sure I've overlooked something with the
syntax of the CONVERT.
This is the staging table:
CREATE TABLE [dbo].[staging_table](
[TimeIndex] [varchar](100) NULL,
[Cluster] [varchar](100) NULL,
[AvgMem] [varchar](100) NULL,
[AvgCPU] [varchar](100) NULL,
[TotalMemory] [varchar](100) NULL,
[TotalCPU] [varchar](100) NULL,
[Datacenter] [varchar](100) NULL,
[vCenter] [varchar](100) NULL
) ON [PRIMARY]
This is the prod table I'm moving it to:
CREATE TABLE [dbo].[Clusters](
[ClusterID] [int] IDENTITY(1,1) NOT NULL,
[ClusterName] [varchar](25) NULL,
[DatacenterName] [varchar](25) NULL,
[TimeIndex] [datetime] NULL,
[AvgCPU] [decimal](5, 2) NULL,
[AvgMem] [decimal](5, 2) NULL,
[TotalCPU] [decimal](8, 2) NULL,
[TotalMem] [decimal](8, 2) NULL,
[vCenterID] [int] NULL,
CONSTRAINT [PK_Clusters_1] PRIMARY KEY CLUSTERED
[ClusterID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
and here's an example INSERT INTO statement throwing the error:
INSERT INTO [dbo].[Clusters] (ClusterName,DatacenterName,TimeIndex,AvgCPU,AvgMem,TotalCPU,TotalMem,vCenterID)
SELECT SUBSTRING(Cluster,1,25) AS ClusterName,
SUBSTRING(Datacenter,1,25) AS DatacenterName,
CONVERT(datetime,TimeIndex,103) AS TimeIndex,
CONVERT(decimal(5,2),AvgCPU) AS AvgCPU,
CONVERT(decimal(5,2),AvgMem) AS AvgMem,
CONVERT(decimal(8,2),TotalCPU) AS TotalCPU,
CONVERT(decimal(8,2),TotalMemory) AS TotalMem,
'3' FROM [dbo].[staging_table]
Sample data is 0.00 to 100.00 in fields AvgCPU and AvgMem, and TotalCPU and TotalMem usually goes up to about 7 digits with no decimal (eg. 7543253) but could be 8 and although I've never seen a decimal I wouldn't rule it out so decided to account for it.
I assume it's something I've overlooked with the syntax but any ideas would help.
Thanks
AdamThe problem is your precision and scale you are assigning to your decimals.
decimal(5,2) = this is a total of 5 digits, 3 digits for the whole number and 2 for the fractional.
decimal(8,2) = this is a total of 8 digits, 6 digits for the whole number and 2 for the fractional.
So converting a varchar of 7 or 8 digits for TotalCPU or TotalMem will give you an error because your definition will actually only allow for 6 digits of storage. You could test this by doing decimal(8,0) or decimal(10,2) both which will allow for up to
8 whole numbers.
If you are worried about space Sql Server will allocate a set number of bytes for ranges based on the precision (first number in the parenthesis). See this page which explains in detail how much space each range takes up and also further details on
decimal and numerics.
-Igor -
Hi
im trying to convert varchar to date.
So, i used To_date ( cilumn, 'DD-MM-YYYY') , Since i have to order the out put in Decending Order i used the same convertion function with Order by clause.
The problem is ... the out put is not ordered,
<sql>
select date_created
from
select distinct
to_date(created_date,'DD-MM-YYYY') as date_created
from schedule_backup
order by date_created desc
<sql>
out put is
=================
DATE_CREA
26-NOV-09
23-NOV-09
19-NOV-09
16-NOV-09
11-NOV-09
04-NOV-09
30-OCT-09
11-SEP-09
04-MAR-10
01-MAR-10
25-FEB-10
DATE_CREA
22-FEB-10
17-FEB-10
15-FEB-10
11-FEB-10
09-FEB-10
03-FEB-10
27-JAN-10
20-JAN-10
15-JAN-10
04-JAN-10
thanks
rajHi, Raj,
When you store dates in a VARCHAR2 column, you're asking for touble, so it seems that everything is working according to plan.
Even aside from that, your query works perfectly for me.
In the output you posted, the order is correct within each year, and you're only showing 2 digits of the year. Are you sure the centuries are right in the schedule_backup table? Perhaps everything that was supposed to be '2010' got enetereed as '1910' or '0010'.
Post some sample data (CREATE TABLE and INSERT statements) that gets the wrong results with your query.
For example:
CREATE TABLE schedule_backup
( created_date VARCHAR2 (10)
INSERT INTO schedule_backup (created_date) VALUES ('11-11-2009');
INSERT INTO schedule_backup (created_date) VALUES ('04-11-2009');
INSERT INTO schedule_backup (created_date) VALUES ('30-10-2009');
INSERT INTO schedule_backup (created_date) VALUES ('11-09-2009');
INSERT INTO schedule_backup (created_date) VALUES ('04-03-2010');
COMMIT; -
Convert VARCHAR to date - don't know format in advance
Hi,
If I want to convert varchar to date, but I don't kniow the format of the varchar in advance. For example, it may be '20-MAY-99', '2009-01-10 10:01:00', but it is not known in advance. How can I convert it to date?
Many thanksYou'll have to define a hierarchy of date formats to try. There are some strings that map to many different valid dates depending on the format. The string '01/02/03' might translate to January 2, 2003, January 2, 1903, February 1, 2003, February 1, 1903, February 3, 2001, February 3, 1901, etc.
Assuming you define a hierarchy of formats, you can write a function that tries each format in turn, i.e.
CREATE FUNCTION my_to_date( p_string IN VARCHAR2 )
RETURN DATE
IS
l_date_variable DATE;
BEGIN
l_date_variable := to_date( p_string, <<format 1>> );
return l_date_variable;
EXCEPTION
WHEN OTHERS THEN
BEGIN
l_date_variable := to_date( p_string, <<format 2>> );
return l_date_variable;
EXCEPTION
WHEN OTHERS THEN
END;You'll need to define the N date formats to try and the order in which to try them.
Justin
Maybe you are looking for
-
Creating remote objects and passing the retrieved data to modules
I found at this Adobe tutorial a nice "RemoteService" class that creates a RemoteObject and contains the functions for handling the result and fault events. If I wanted to use this approach, how could I pass the data from the result handler to int
-
Hi Does anyone know if its possible to query data based on direction eg. select all objects that are east of object A. Thanks K
-
SDK: adding new value in custom fields
Hello, I wanted to add new value in custom field of the resource table... I used this code (taken from SDK example) : //Create and fill request for CustomData Service SaveCustomDataRequest request = new SaveCustomDataRequest(); List<CustomValue> cust
-
Add field group to Asset Master Data Screen Layout
Hi Gurus !! Does anybody know how to add field group in SPRO(Define Screen Layout for Asset Master Data)?. I have created my layout by coping 1000, but then I need to add Fund Center in Time-Dependent data logical field group. Thanks a lot, Sasha
-
Everything went smooth with Oracle 8 setup until svrmgl. I believe that oracle was trying to look up the error text. But could not get the error text so displayed another error. So there is actually 2 errors here. Anyone have any ideas.... ----- Star