Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Insert - (hyphens) Into a Date String

Status
Not open for further replies.

JohnBates

MIS
Feb 27, 2000
1,995
US
hi,

This char date is like 20040214 with no dashes, slashes.

If I can insert dashes to get 2004-02-14 then it will convert to a DateTime data type in the DTS transformation task.

Just need a little tip on how to insert the dashes with code.

Thanks, John
 
... and I should have explained that I actually want to update the row with the new char value of 2004-02-14 for example.

John
 
Update table
SET yourfield = LEFT(Yourfield,4)+-+SUBSTRING(Yourfield,5,2)+-+Right(Yourfield,2)

You could use substring in all three occasions above but thought would put in left and right to show their use.

Substring explained is the field you are interested in the start position of what you want and the length of what you want.

Hope this helps.

DBomrrsm
 
Or you could use this:

Code:
declare @Date as CHAR(10)

SET @Date = CONVERT(CHAR(10),CONVERT(DATETIME,'20040214'),105)

PRINT @Date

The above example will show you how it works, but you will want it to look like

Code:
SET yourfield = CONVERT(CHAR(10),CONVERT(DATETIME,yourfield),105)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top