T-SQL (SS2K8) :: Using CAST Or CONVERT To Change Data From String To Datetime?

Apr 16, 2014

i am trying to take a field that has part of a date in it, so I have to parse it out as follows:

SUBSTRING(a1.Field1, 3, 2) + SUBSTRING(a1.Field1,5,2) + '20' + LEFT(a1.Field1,2)

This is because a date of 04/16/2014 will show as 160416 in the first part of the field I need to parse it out of, thus becoming 04162014.

From there I then need to convert this "date" into a legitimate SQL datetime type, so that I can then run a DATEDIFF to compare it to when the record was actually entered, which is a separate field in the table, and already in datetime format.

When I use the below statement, I am getting the message that, "Conversion failed when converting date and/or time from character string."

CAST((SUBSTRING(a1.Field1, 3, 2) + SUBSTRING(a1.Field1,5,2) + '20' + LEFT(a1.Field1,2)) as datetime)

I also tried CONVERT(datetime, (SUBSTRING(a1.Field1, 3, 2) + SUBSTRING(a1.Field1,5,2) + '20' + LEFT(a1.Field1,2)), and got the same message.

how I can parse that field, then convert it to a datetime format for running a DATEDIFF statement?

View 9 Replies


ADVERTISEMENT

SIMPLE Command To Convert String To Number? Not CAST Or CONVERT.

Aug 15, 2006

Dear Experts,Ok, I hate to ask such a seemingly dumb question, but I'vealready spent far too much time on this. More that Iwould care to admit.In Sql server, how do I simply change a character into a number??????In Oracle, it is:select to_number(20.55)from dualTO_NUMBER(20.55)----------------20.55And we are on with our lives.In sql server, using the Northwinds database:SELECTr.regionid,STR(r.regionid,7,2) as a_string,CONVERT(numeric, STR(r.regionid,7,2)) as a_number,cast ( STR(r.regionid) as int ) as cast_to_numberFROM REGION R1 1.00112 2.00223 3.00334 4.0044SELECTr.regionid,STR(r.regionid,7,2) as a_string,CONVERT(numeric, STR(r.regionid,7,2) ) as a_number,cast (STR(r.regionid,7,2) as numeric ) as cast_to_numberFROM REGION R1 1.00112 2.00223 3.00334 4.0044Str converts from number to string in one motion.Isn't there a simple function in Sql Server to convertfrom string to number?What is the secret?Thanks

View 4 Replies View Related

How To Convert String Data Type To DateTime In Derived Column Control In SSIS Package

Jun 26, 2006

Hi ,

I am Using Derived column between Source and Destination Control. the Source input column PriceTime is String Data type. but in the Destination is should be a DATE TIME column. How to Convert this string to DateTime in the Derivied Column Control.

I already tried to in the Derived column control

PRICEDATETIME <add as new column> ((DT_DBTIMESTAMP)priceDateTime) database timestamp [DT_DBTIMESTAMP]



But still throwing Error showing type case probelm



Pls help me on this



Thanks & Regards

Jeyakumar.M






View 23 Replies View Related

Convert Datetime String To Datetime Date Type

Mar 11, 2014

I am inserting date and time data into a SQL Server 2012 Express table from an application. The application is providing the date and time as a string data type. Is there a TSQL way to convert the date and time string to an SQL datetime date type? I want to do the conversion, because SQL displays an error due to the

My date and time string from the application looks like : 3/11/2014 12:57:57 PM

View 1 Replies View Related

T-SQL (SS2K8) :: Try Convert Causes Change In Transaction State

Mar 16, 2015

Why does this code (below) cause a change in the transaction state?

DECLARE @date DATE,
@string VARCHAR(5)= 'test'
BEGIN TRAN
PRINT XACT_STATE()

[Code] ....

View 8 Replies View Related

T-SQL (SS2K8) :: Convert Datetime To W3C Date And Time Format

Dec 21, 2014

I have a standard datetime and I need to convert it to the client specification of:

YYYY-MM-DDThh:mm:ssTZD
eg: 2009-04-16T19:20:30+08:00

I am not sure of the easiest way to do this.

The test code below gets me part of the way but I am unsure on how to get the offset on the end without hardcoding to much.

DECLARE @datetime DATETIME = '2014-12-20 12:30:00'
SELECT CONVERT(VARCHAR(30),@datetime,127)

View 2 Replies View Related

T-SQL (SS2K8) :: How To Concatenate / Convert Date And Time Into Datetime

Aug 3, 2015

We have 2 columns:

StartDate - data type Date
StartTime - data type Time

I need to combine them into a DateTime data type. For now, I convert each of them into varchar, insert space in between, and convert to DateTime, like this:

convert(datetime, convert(varchar(10), EndDate , 101) + ' ' + CONVERT(varchar(20), EndTime,24))

Is there a better, more elegant way to do this?

View 9 Replies View Related

How To Convert String To DateTime

Jan 2, 2006

hi , i have a problem
i have an textarea that i want to convert to DateTime format (dd/MM/yyyy) .
the data in the textarea is (dd/MM/yyyy for example 21/12/2005).
i need it to add this data in sql server , in smalldatetime formation colum .
plz help.
 
 

View 2 Replies View Related

T-SQL (SS2K8) :: Convert String To Date?

Sep 5, 2014

I'm importing dates into a table with Bulk insert

SET DATEFORMAT DMY

it works with dates e.g. "14/01/2009"

However sometimes I get dates in the format

"Fri 14/01/2009"

What is the best way to convert these.

I can only think of putting them in a staging table with all date fields as varchars Then updating these varchar fields

LTRIM(REPLACE(dtField, 'Mon', ''))
LTRIM(REPLACE(dtField, 'Tues', ''))

View 9 Replies View Related

Convert Date-string To Datetime

Jan 6, 2015

I am trying to convert a date-string of this format (DDMMMYY - ex. 06JAN15) to datetime but I keep on getting errors:

codes that I tried:
convert(datetime, '06JAN15', 6)
convert(datetime, '06JAN15', 112)

What date format code should I use for DDMMMYY?

View 13 Replies View Related

Concatenate String Then Convert To Datetime

Jan 23, 2008

I've been trying to do the following:





Code Snippet
if year(@SomeDateTime) <> @SomeYear

set @SomeDateTime= convert(datetime, @SomeYear+ '1231', 112)


If variable @SomeDateTime evaluates to 20080101 and @SomeYear = 2006, I wish that my variable @SomeDateTime becomes 20061231 (December 31st).

The way it's written now, it doesn't work... @SomeDateTIme evaluates to 1908-11-12.... ?!





View 6 Replies View Related

Convert String To Datetime - Performance

Apr 12, 2007

Dear Friends,
I have a doubt relation when converting a field€¦
I €˜m converting a string to datetime inside a SQL command in an OLE DB Source using this:
convert(datetime,[Maturity],103) As MaturityDate
It€™s better to use inside the OLE DB Source or is better to convert it with data conversion transformation??
Regards!!
Thanks

View 11 Replies View Related

Transact SQL :: Convert String To Datetime

Jul 28, 2015

I have below string, which is a datetime value

I want to convert it into datetime data type and insert into table

DECLARE @Date VARCHAR(100)

SET @Date='10312013122642'

View 8 Replies View Related

Convert SSIS DateTime To A String

May 13, 2008

Being a newbie to SSIS I'm not sure of the most efficient method of converting a DateTime object to a String.

I'm from a C# background where this would be easy using DateTime.ToString("YYYYMMdd"). I want to use the date in a file name so don't require most of the parts.

I'm sure I could do this using a script task to produce a file name for each row of data in my table and add that filename to the dataset but it seem like overkill to do something that should be simple. Also as I'm supposed to be getting to grips with SSIS I shouldn't keep running back to what I know.

My current approach is to derive a column and build up an expression to convert the date into a string. The only problem being that it doesn't work.
The expression I'm working with is:
(DT_WSTR, 50)([OrgName] ) + "_" + (DT_WSTR, 50)( [PayrollName] ) + (DT_WSTR, 4)(YEAR( [ProcessedDate] )) + (DT_WSTR, 2)(MONTH( [ProcessedDate] )) + (DT_WSTR, 2)(DAY( [ProcessedDate] )) ".txt"

Can anyone see where I'm going wrong?

All comments greatly received.

Cheers
Ben

View 8 Replies View Related

T-SQL (SS2K8) :: Error Converting Datetime To String

May 29, 2014

I am trying to create a stored procedure that Pulls in Chargeable and Non Chargeable hours for our employees however When I run the Stored Procedure I get this error "Conversion failed when converting date and/or time from character string." I am having a hard time figuring out were this is happening in the Stored Procedure. Also I would like to be able to Add a parameter that would be the StartDate and EndDate for which the stored procedure would pull time for.

ALTER PROCEDURE [dbo].[Chargeability]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

[Code] ......

View 9 Replies View Related

Cast/Convert Mmddyy In String To New DB_DATETIMESTAMP Column In Derived Column Transformation

Mar 5, 2007

Hi,
I have dates in "mmddyy" format coming from the sources and they are older dates of mid 80s like 082580 for instance.

When I cast it this way (DT_DBTIMESTAMP) Source_Date , It says ok but throws a runtime error.

When I hardcode a date in same format, (DT_DBTIMESTAMP) "082580" , It becomes red (an indication of syntax error) . Please note that we use double quotes in expressions in Derived Column Transformation; So an anticipation that using double quotes over single ones would be the syntax problem would be wrong.



Any help in this will sincerely be appreciated.


Thanks

View 7 Replies View Related

Convert A Date Stored As A String Into A Datetime

May 28, 2007

Hello forum,
Is it possible to convert a date stored as a string into a datetime with integration services 2005? My attempts with the €œdata conversion€? fail. The string type form of the date is €˜yyyy-mm-dd€™ and the desired result for use in a Union All is €˜dd/mm/yyyy 12:00:00AM.€™ This outcome is needs so that match on the date can populate a fact table, as the results are coming from two different databases.
All advice/help welcomed.
Ian

View 4 Replies View Related

Failed To Convert Parameter Value From A String To A Datetime

Nov 1, 2007

Hi I am having this problem. I have created a stored proc that uses dynamic sql. Now this works fine when I use it in Query Analyzer.

Now when I try to use this in Reporting services it gives me the error:
'
Failed to generate fields for the query
Check the query syntax or click refresh on query toolbar.
Failed to convert parameter value from a string to a datetime
'

My sotred proc is as:

<quote>
-------------------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[rptDetailedAuditsIssued]

(

@param1 varchar(50) ,

@param2 varchar(50),

@param3 varchar(50),

@param4 datetime ,

@param5 datetime



)

AS

begin

declare @strSQL varchar(500)

set @strSQL = 'SELECT * FROM v_DetailedAuditsIssued WHERE '

if @param1 <> ''

Begin

set @strSQL = @strSQL + 'strRegion = ' + @param1 + ' AND '

End



if @param2 <> ''

Begin

set @strSQL = @strSQL + ' strTaxTypeCode = ' + @param2 + ' AND '

End



if @param3 <> ''

Begin

set @strSQL = @strSQL + ' strAuditType = ' + @param3 + ' AND '

End

set @strSQL = @strSQL + ' (dtmIssuedDate BETWEEN '''

+ CONVERT(nvarchar(30), @param4, 101) + ''' AND '''

+ CONVERT(nvarchar(30),@param5, 101) + ''')'



exec (@strSQ



end
--------------------------------------------------------------------------------------------------------------------------
</quote>

When I run


rptDetailedAuditsIssued '','','','1/1/2000','1/1/2001'in query editor it runs fine. But gives me that error every time I run it in SSRS. I think I got this problem but in earlier cases I used to set the param values to null in Repservices and it worked but its not working this time. This time it stops complaining but does generate any fields.

View 1 Replies View Related

T-SQL (SS2K8) :: Convert Binary String To Table Value Construct?

Mar 21, 2014

Code snippet for changing '00001001001' into a table value construct containing (64,8,1)?

View 6 Replies View Related

SQL Server 2014 :: Function To Convert Datetime To String

Jan 26, 2014

Writing a SQL Function as below

the input parameter for function should be datetime of sql datetimeformat

and out put should be a string = yyyymmdd1 or yyyymmdd2

The last character 1 or 2 based on below condition

if time is between 6AM and 5.59PM then 1
if 6PM to 5.59AM then 2

View 6 Replies View Related

T-SQL (SS2K8) :: Convert Unique Identifier From String To Use In A Join / Where Clause

Jan 6, 2015

I am wanting to get the job name based on sys.sysProcesses.[Program_name] column. Why is this query not returning any results even though the 2nd substringed guids are found the the sysJobs table?

SELECTCASE
WHEN RTRIM([program_name]) LIKE 'SQLAgent - TSQL JobStep (Job %' THEN J.Name
ELSE RTRIM([program_name])
END ProgramName
, Val1.UqID
, Val1.UqIDStr

[Code] ......

View 6 Replies View Related

Cannot Convert Yymmdd Unicode Date String To SQL Server DateTime

Jan 28, 2008



Hello,
I have tried a few different things within a Derived Column transform to convert a unicode yymmdd date string to SQL Server DateTime.

Does anyone have ideas on how to do this?

Thank you for your help!

cdun2

View 4 Replies View Related

Unable To Cast Object Of Type 'System.DateTime' To Type 'System.String'.

Dec 31, 2007

 Hi,      I got this field (dateSubmitted) having a data type of DateTime but I receive this error "Unable to cast object of type 'System.DateTime' to type 'System.String'."       All value for dateSubmitted field are 12/27/2007 12:00:00 AM. cheers,imperialx 

View 3 Replies View Related

T-SQL (SS2K8) :: Convert Column Data Into Comma Separated Data?

Mar 14, 2014

I have data in the below format .

NameValuecategory
AAA510
BBB510
CCC510
DDD512
EEE512
FFF512

I want the result in the below format

NAMEValuecategory
AAA,BBB,CCC510
DDD,EEE,FFF5120

I have tried stuff but all six values(AAA...FFF) are coming in one row , however i need them as per the category.

View 2 Replies View Related

Convert Character Data To Datetime

Aug 16, 2006

Hi! I need to insert character data into a SQL 2005 table in the datetime format. The strings I need to convert are in the following format: ddmmyyyy

I tried using case and convert:

cast('08162006' as datetime))
convert(datetime,'08162006',101))

Both attempts fail with the following error:

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

Any advice is greatly appreciated. Thanks.

View 10 Replies View Related

DB Design :: Cannot Change Datetime Default Data Type

Nov 17, 2015

Ilve install sql server 2012 in my pc and i want to change datetime default format. How can i do this and please i dont want to take the result from select convert() or select cast or something like this. I ve want to take the format i want writing query select datecolumn from table. 

Now the format i have is: 2015-11-16 09:04:00.000

And i want this format: 16.11.2015 09:04:00

Is any way to convert automaticaly by select only column? or can i change at all once? or must write function to when i select the column can change automatic ? or another thing, i ve see in column properties something like formula. In computed column specification in formula i wrote this: 

((CONVERT([varchar](10),getdate(),(104))+' ')+CONVERT([varchar](10),getdate(),(108)))

And I take the format i want automaticaly but i get the current date for all rows and i cant edit or insert that column anymore. So, how to change the format of date time but no from select query.

View 4 Replies View Related

T-SQL (SS2K8) :: Pivot Query - Convert Data From Original Table To Reporting View

Apr 8, 2014

I want to convert the data from Original Table to Reporting View like below, I have tried but not get success yet.

Original Table:
================================================================
Id || Id1 || Id2 || MasterId || Obs ||Dec || Act || Status || InstanceId
================================================================
1 || 138 || 60 || 1 || Obs1 ||Dec1 || Act1 || 0|| 14
2 || 138 || 60 || 2 || Obs2 ||Dec2 || Act2 || 1|| 14
3 || 138 || 60 || 3 || Obs3 ||Dec3 || Act3 || 1|| 14
4 || 138 || 60 || 4 || Obs4 ||Dec4 || Act4 || 0|| 14
5 || 138 || 60 || 5 || Obs5 ||Dec5 || Act5 || 1|| 14

View For Reporting:

Row Header:
Id1 || Id2 || MasterId1 || Obs1 ||Desc1 ||Act1 ||StatusId1||MasterId ||Obs2 ||Desc2 ||Act2 ||StatusId2 ||MasterId3||Obs3 ||Desc3 ||Act3 ||StatusId3||MasterId4||Obs4||Desc4 ||Act4 ||StatusId4 ||MasterId5||Obs5 ||Desc5 ||Act5 ||StatusId5||InstanceId

Row Values:
138 || 60 || 1 || Obs1 ||Desc1 ||Act1 ||0 ||2 ||Obs2 ||Desc2||Act2 ||1 ||3 ||Obs3||Desc3 ||Act3 ||2 ||4||Obs4||Desc4 ||Act4 ||0 ||5 ||Obs5 ||Desc5 ||Act5 ||1 ||14

View 6 Replies View Related

Convert Blob Data To String

Aug 25, 2006

How do I use the GetBlobdata to get the string representation?

I have tried numerous version of Encoders thinking that

System.Text.Encoding.GetEncoding(Row.TextData.ColumnInfo.CodePage).GetString(b) (where b is the byte array returned by GetBlobData)

But this doesn't work. I have had to resort to converting the field in SQL, so I don't have to deal with blobdata.

View 4 Replies View Related

Convert To String From Binary Data

Mar 31, 2008

hello all,
I am reading a file that contains textual data (formatted,e.g font=bold,font size=10 etc). After reading the file, i store the data read from the file to a table in Sql server 2005. But in the table, this data is stored like this:

daohORIGINAL TEXTm............

how can i just store the original text of the file in the table and get rid of these boxes? Or when i read this data from table, how to remove these boxes and get only the original text?
Thanks in advance.
I am using C# FileStream to read the file from disk. VS 2005

View 25 Replies View Related

Cast Or Convert

Feb 18, 2008

 Hi,I want to turn int to double/decimal  in microsoft sqlSHould i use cast or convert?if so, how i do it thanks, 

View 1 Replies View Related

CAST/Convert

Mar 15, 2006

I need Query syntax to cast/convert values as follws.

Val.: 00005000010260002180 - Result must be: 5.1.2600.2180
Val.: 00005000000213400001 - Reslut must be : 5.0.2134.1

Dots must also be contained in result

View 2 Replies View Related

CAST Or Convert

Oct 8, 2014

Have the following in my SELECT statement, which I'm having issues with when I modify it to include a CAST or a CONVERT.

CONCAT(PER.[PERSON-REF],ROW_NUMBER() OVER (PARTITION BY PER.[PERSON-REF] ORDER BY TEN.[tenancy-ref])) AS 'ID'How do I convert or cast it to a varchar (20)?

View 8 Replies View Related

Convert AND Cast

Nov 6, 2007

We have a 3rd party application that connects to our SQL 2005 as a external database source.

Our external database has a 'datein' field that for some reason is stored as a varchar(50).

I can use cast to convert the field to datetime, but how would I then format to the yyyy-mm-dd format in the same select statement(using in view)?

I need to convert this field to a format of YYYY-MM-DD in order for the 3rd party application to search on it correctly.
Below is an example of the data --
FieldName = DateIn
DateType = Varchar(50)
2006-07-06
Sep 12 2007 11:16AM
2007-07-19
Oct 2 2007 12:24PM
2007-08-19
Oct 31 2007 8:00PM
Sep 15 2007 9:25AM
2007-08-18
Oct 16 2007 7:01PM
Oct 30 2007 11:15AM
Aug 29 2007 8:54AM
Aug 29 2007 8:38PM
Aug 25 2007 2:33PM

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved