Home All Groups Group Topic Archive Search About
Author
18 Aug 2006 2:13 PM
amjad
is their any function in sql server to format date like
ddmmyyyyhhmm i want to create a key from date but date is bydefault like
12/12/2004 12:30:30 but i want to convert it to like we do in vb
format("ddmmyyyyhhmm",date)... thanks

Author
18 Aug 2006 2:21 PM
Aaron Bertrand [SQL Server MVP]
> i want to create a key from date

Bad move!

A
Author
18 Aug 2006 2:27 PM
lord.zoltar
amjad wrote:
> is their any function in sql server to format date like
> ddmmyyyyhhmm i want to create a key from date but date is bydefault like
> 12/12/2004 12:30:30 but i want to convert it to like we do in vb
> format("ddmmyyyyhhmm",date)... thanks


The CONVERT function can convert many things... datetimes among them.

You could  it like this:

"DECLARE @myDate datetime
SET @myDate=GETDATE()
SELECT CONVERT(datetime, @myDate, 120)"

This link has more information about CONVERT:
http://msdn2.microsoft.com/en-us/library/ms187928.aspx
Author
18 Aug 2006 2:29 PM
Chris Strug
"amjad" <am***@discussions.microsoft.com> wrote in message
news:47ED88D8-59AB-4C34-A831-1DD8C3C6601E@microsoft.com...
> is their any function in sql server to format date like
> ddmmyyyyhhmm i want to create a key from date but date is bydefault like
> 12/12/2004 12:30:30 but i want to convert it to like we do in vb
> format("ddmmyyyyhhmm",date)... thanks

Hi

Couple of points from my limited experience.

1. Using a datetime as a key field is asking for trouble.
2. Typically, formatting a datetime is done on the client rather than the
database engine - however it is possible to use the CONVERT function to
change the format of a datetime if its required.

I hope this helps

Thanks

Chris.
Author
18 Aug 2006 3:18 PM
amjad
i am not using just datetime field for key field but combing with another
fields... weather its a bad move or good move as long its work for me i dont
care....
please if you dont know then dont say bad movessss. you dont my work. thanks


Show quote
"Chris Strug" wrote:

> "amjad" <am***@discussions.microsoft.com> wrote in message
> news:47ED88D8-59AB-4C34-A831-1DD8C3C6601E@microsoft.com...
> > is their any function in sql server to format date like
> > ddmmyyyyhhmm i want to create a key from date but date is bydefault like
> > 12/12/2004 12:30:30 but i want to convert it to like we do in vb
> > format("ddmmyyyyhhmm",date)... thanks
>
> Hi
>
> Couple of points from my limited experience.
>
> 1. Using a datetime as a key field is asking for trouble.
> 2. Typically, formatting a datetime is done on the client rather than the
> database engine - however it is possible to use the CONVERT function to
> change the format of a datetime if its required.
>
> I hope this helps
>
> Thanks
>
> Chris.
>
>
>
Author
18 Aug 2006 3:25 PM
Roy Harvey
Are you familiar with the idea of giving someone enough rope with
which to hang themselves?

I hope this isn't rope.

select LEFT(REPLACE(convert(varchar(30),getdate(),104),'.','') +
            REPLACE(convert(varchar(5),getdate(),114),':',''),
            12)

There are plenty of times when a datetime column is a key, or part of
a key.  I certainly would not want a character string such as the
above used as a key.  Formatting of dates, as has been said by others,
is better handled by the front-end program.

Roy Harvey
Beacon Falls, CT

On Fri, 18 Aug 2006 07:13:28 -0700, amjad
<am***@discussions.microsoft.com> wrote:

Show quote
>is their any function in sql server to format date like
>ddmmyyyyhhmm i want to create a key from date but date is bydefault like
>12/12/2004 12:30:30 but i want to convert it to like we do in vb
>format("ddmmyyyyhhmm",date)... thanks
Author
18 Aug 2006 3:47 PM
amjad
thanks i have done exactly you show to me... but i was looking in one
function... i dont have front end as i can do it with c# and vb.net... but i
am writing a store proc.......
thanks any way

Show quote
"Roy Harvey" wrote:

> Are you familiar with the idea of giving someone enough rope with
> which to hang themselves?
>
> I hope this isn't rope.
>
> select LEFT(REPLACE(convert(varchar(30),getdate(),104),'.','') +
>             REPLACE(convert(varchar(5),getdate(),114),':',''),
>             12)
>
> There are plenty of times when a datetime column is a key, or part of
> a key.  I certainly would not want a character string such as the
> above used as a key.  Formatting of dates, as has been said by others,
> is better handled by the front-end program.
>
> Roy Harvey
> Beacon Falls, CT
>
> On Fri, 18 Aug 2006 07:13:28 -0700, amjad
> <am***@discussions.microsoft.com> wrote:
>
> >is their any function in sql server to format date like
> >ddmmyyyyhhmm i want to create a key from date but date is bydefault like
> >12/12/2004 12:30:30 but i want to convert it to like we do in vb
> >format("ddmmyyyyhhmm",date)... thanks
>
Author
18 Aug 2006 3:57 PM
Aaron Bertrand [SQL Server MVP]
> thanks i have done exactly you show to me... but i was looking in one
> function... i dont have front end as i can do it with c# and vb.net... but
> i
> am writing a store proc.......
> thanks any way

So why can't you create a function????


USE tempdb
GO
CREATE FUNCTION dbo.HangYourself
(
@dt SMALLDATETIME
)
RETURNS CHAR(12)
AS
BEGIN
RETURN
(
SELECT LEFT
(
REPLACE(convert(varchar(30),@dt,104),'.','') +
REPLACE(convert(varchar(5),@dt,114),':',''),
12
)
)
END
GO
SELECT dbo.HangYourself('20060812 3:45');
GO
DROP FUNCTION dbo.HangYourself;
GO
Author
18 Aug 2006 4:01 PM
Tracy McKibben
Aaron Bertrand [SQL Server MVP] wrote:
>> thanks i have done exactly you show to me... but i was looking in one
>> function... i dont have front end as i can do it with c# and vb.net... but
>> i
>> am writing a store proc.......
>> thanks any way
>
> So why can't you create a function????
>

Because Roy didn't do it for him...  :-)


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
18 Aug 2006 4:05 PM
Aaron Bertrand [SQL Server MVP]
>> So why can't you create a function????
>>
>
> Because Roy didn't do it for him...  :-)

Sorry, my bad!  Next we're going to get server credentials so we can run the
CREATE FUNCTION script, too.  :-)

AddThis Social Bookmark Button