Date Time and VARCHAR

Hi,
I have a variable like @OrderTime varchar(8) now this variable I am using to add a timestamp to a file. Like SET @FileName = 'xyz' + @OrderTime.+ '.xml'
My file should look like this xyz20101229130446470.xml . How Can I do that? I want everything except the millisecond part so 470 will be removed and it should be xyz20101229130446.xml
ExpertKapurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sammySeltzerCommented:
Try this and see if it helps:

I@FileName = 'xyz' + SUBSTRING(CONVERT(varchar, @OrderTime, 100), 13, 5)+ '.xml'

Open in new window

Som TripathiDatabase AdministratorCommented:
Please try below -

Declare @OrderTime varchar(14) 
Declare @FileName varchar(21) 

Set @OrderTime = replace(replace(replace(convert(varchar , getdate(), 120) ,'-',''), ' ',''), ':','') 

Set @FileName = 'xyz' + @OrderTime + '.xml'
Print @FileName 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HainKurtSr. System AnalystCommented:
try

select @OrderTime  = replace(replace(replace(convert(varchar(19),getdate(),21),'-',''),':',''),' ','')
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

sammySeltzerCommented:
Actually this is better:

select FileName = 'xyz' + CONVERT(CHAR(10),@OrderTime, 23) + SUBSTRING(CONVERT(varchar, @OrderTime, 100), 13, 5)+ '.xml' from yourtable

Open in new window

sammySeltzerCommented:
Guys, I said "better" compared to the original code I posted; not to you guys code.

Just wanted to clarify to avoid confusion.
ExpertKapurAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.