Solved

How to convert a date to a certain string

Posted on 2013-11-25
3
401 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
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 65

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 48

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now