Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CAST AND CONVERT T-SQL

Posted on 2012-03-29
2
Medium Priority
?
346 Views
Last Modified: 2012-03-29
Trying to take a two step process down to one step.  I'm if got a date stored as 20110119(YYYYMMDD), this date represents  Jan 19, 2011.  

Currently, I'm using a data step approach using a WITH statement.

WITH T1 AS
(
Select  CAST(FromDate AS DATE) AS FmDt
From MyTable
/* this takes it from 20110119 to 2011-01-19 */
)

Select CONVERT(NVARCHAR(110), T1.PgmFmDt, 110) AS PgmFmDt
/* this takes it from 2011-01-19 to 01-19-2011 */

What I really want to do is go direct from 20110119 to 01/19/2011, notice I'm also want to replace the - with /.    

Your thoughts?
0
Comment
Question by:P19_3141
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 2000 total points
ID: 37782698
Pretty sure this is all you need to do

Select  CONVERT(nvarchar(30),CAST(FromDate As DateTime), 101) AS FmDt
From MyTable
0
 

Author Closing Comment

by:P19_3141
ID: 37782840
After a day of playing with it on my own.......The answer magically appears on EE in under 5 mins.  

That did the trick.  Thanks!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question