all groups > sql server programming > july 2006 >
You're in the

sql server programming

group:

Getting date in own format like yyyy/dd ...


Getting date in own format like yyyy/dd ... sridhar mamidi
7/11/2006 11:30:13 PM
sql server programming:

HI

My problem is that I have a table with date column in it. I want to
retrieve the date value in a particular format which is of date type.

For this I used the CONVERT function but I am getting the following
error:

The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.

The query I used :

SELECT CONVERT(datetime, CONVERT(varchar(6), base_table."date1", 112),
112)

Thanks in advance

Sridhar Mamidi



Re: Getting date in own format like yyyy/dd ... Kalen Delaney
7/11/2006 11:41:26 PM
Hi Sridhar

Is this the exact query you are using? If so, SQL Server can't figure out
what base_table."date1" means.

Also, you are losing all you've gained by converting back to datetime. It's
already stored in the database as datetime, and datetime values always are
displayed in a format determined by the system, not by you. You can only
control the format when you convert datetime to a character string and then
display character data. The system does not try to do any further conversion
on character strings.

If you need to get the date value from a table, something like this instead:

SELECT CONVERT(varchar(6), date1, 112)
FROM base_table

--
HTH
Kalen Delaney, SQL Server MVP


[quoted text, click to view]

Re: Getting date in own format like yyyy/dd ... Aneesh
7/12/2006 12:00:00 AM
You need to convert it to only varchar(8)
SELECT CONVERT(varchar(8), base_table.date1, 112)


[quoted text, click to view]

Re: Getting date in own format like yyyy/dd ... Tav
7/12/2006 3:33:12 AM
Hi Sridhar,
Just to add the icing on the cake (so to speak) the title of your post
states a format like "yyyy/dd" (i.e. with the slash). To do this try:

SELECT STUFF(CONVERT(char(6), base_table.date1, 112), 5, 0, '/')

-Tav.-

Tavis Pitt
Re: Getting date in own format like yyyy/dd ... Lawrence
7/12/2006 6:14:02 PM
I have another formating question.

Goal - keep column in datatime instead of convert to varchar
Current - '2006-06-21 16:54:33.000'
Wants - '2006-06-21 16:54:33'

Any help is appreciated!
-Lawrence

[quoted text, click to view]
Re: Getting date in own format like yyyy/dd ... Tav
7/13/2006 12:56:56 AM
Lawrence,

SELECT CONVERT(char(19), '2006-06-21 16:54:33.000', 120)

----
-Tav.-
Tavis Pitt
Re: Getting date in own format like yyyy/dd ... Lawrence
7/13/2006 11:31:02 AM
YES, but this would be in char. I would like to keep the datatype to
datetime, so the question would be whether there is a way to format datetime
the same format as what we see in varchar or char.

Thanks,
-Lawrence

[quoted text, click to view]
Re: Getting date in own format like yyyy/dd ... Roy Harvey
7/13/2006 2:59:03 PM
It is up to the front end to format a datetime for display. If you
can not control it as you wish in the front end, then your alternative
is to convert it to a string using the optional third parameter of
CONVERT to control how it looks.

Roy Harvey
Beacon Falls, CT

On Thu, 13 Jul 2006 11:31:02 -0700, Lawrence
[quoted text, click to view]
Re: Getting date in own format like yyyy/dd ... Tav
7/14/2006 2:13:56 AM

[quoted text, click to view]

Lawrence,

The datetime and smalldatetime datatypes in SQL Server are excellent
for date storage and manipulation and should always be used in
databases to store dates. As Roy Harvey explained it is up to the
front end to format a datetime for display or you could save the
application developer some heartache by formatting the date to a char
in the data logic layer (stored procedures). If the application needs
a date it should be sophisticated enough to accept the datetime or
smalldatetime datatype from SQL Server. If it does, the change the
application/object so it does.

-Tav.-
Tavis Pitt
AddThis Social Bookmark Button