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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Special Date/Interger Question

Status
Not open for further replies.

nutrotek

Technical User
Aug 10, 2006
8
US
Hello,

I know how to take these two fields:
DateValue, 01/01/2007 and IntValue, 10 and create one field: 010107-10

REPLACE(CONVERT (VARCHAR(8), DateValue, 1), '/', '') + N'-' + CAST(IntValue AS VARCHAR(3))

How to I reverse the process, turn 010107-10 into 01/01/2007 and 10?
 
The position of the dash is
Code:
CHARINDEX('-', '010107-10')
CHARINDEX returns 7.

The first string is
Code:
SUBSTRING('010107-10', 1, (CHARINDEX('-', '010107-10')-1)  )

The second string is
Code:
SUBSTRING('010107-10', (CHARINDEX('-', '010107-10')+1), DATALENGTH('010107-10'))

Let @strDate be the first string. Then
Code:
SELECT CAST(@strDate AS DATETIME)
SELECT CONVERT(VARCHAR(20), CAST(@strDate AS DATETIME), 101)
will get you there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top