Solved

How to convert a date to a certain string

Posted on 2013-11-25
3
407 Views
Last Modified: 2013-11-26
I have a date in the format 11/14/13 22:28

I need to convert it into 7884111413 in Sql Server query.

where 7884 is a number that I need to put in front of the date and 111413 is the date

thanks in advance
0
Comment
Question by:damixa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Author Comment

by:damixa
ID: 39675940
I was able to do this

(CAST ('7884' as varchar) + cast(convert(varchar, trans_date,12)as varchar))

Open in new window


my only problem is that type 12 gives me the year at the begining and not at the end.

Anybody knows how to get the year at the end (so ddmmyy instead of yymmdd)
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 39675979
Looking through PortletPaul's article on SQL Date Types I didn't see ddmmyy with no delimeters anywhere, so let's roll our own...
Declare @dt datetime = '11/14/13 22:28'

SELECT '7884' +
	RIGHT('0' + CAST(DATEPART(d, @dt) as varchar(2)),2) + 
	RIGHT('0' + CAST(DATEPART(m, @dt) as varchar(2)),2) + 
	RIGHT(CAST(YEAR(@dt) as char(4)),2) 

Open in new window

0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 39676433
11/14/13 22:28
as there cannot be a 14th Month this appears to be MM/DD/YY

but you then ask:
Anybody knows how to get the year at the end (so ddmmyy instead of yymmdd)

choose the one you want from these:
Declare @dt datetime = '20131114 22:28'

SELECT
  '7884' + replace(convert(varchar, @dt ,3),'/','') as "7884 ddmmyy"
, '7884' + replace(convert(varchar, @dt ,1),'/','') as "7884 mmddyy"

Open in new window

0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

691 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