Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

Friday, March 30, 2012

Return datetime type variable from SP

How can I return a datetime type variable from a stored procedure in SQL Server to C# code?

Hello,

I don't know if I got it right, but to return anything from a Stored procedure just make something like:

Select client_datecreated from clients

if you need a date put it as a field then in C# execute the command and use the sqldatareader class:

check msdnhere

The command can be a Stored as well as a query.

|||Do i need to set an output parameter in the SP? The syntax in the SP is confusing me.|||

select convert(char(10),fieldname in table,101) as test_Date from tablename

the 101 is a code which gives the date in mm/dd/yyyy format.

You should check with 'books online" in your SQL server help section.that gives you a list of different format you may want your date to be in.

convert basicly is truncating your date to have 10 characters otherwise you will have the hours:minute:seconds too in your result.

|||

If you would like to get data as return parameter (not return Value which is always int) you have to define it as OUTPUT in stored procedure definition, and also you have to setup this parameter as output in your SQLcommand object parameters definition.

If you would like to return it as cell in result table you do not have to define parameter and you can just do select yourdatafied from yourtable at the end of your stored procedure.

But SQL Command with output parameter is more elegant solution and will work a little faster ( .net do not have to create table structure for returned data)
and you can use executeNonQuery instead of execute scalar or execute reader.

See VB or C# help for syntax how to do this if you will have problems post again, but help is very good in VS so you should be good.

Return Date not DateTime

I am trying to count the amount of distinct dates (not datetime) in a table row. The call below returns the amount of distinct datetimes. How do I strip off the time when doing the SQL call?

SELECT COUNT(DISTINCT DT) FROM Event

SELECTConvert(Varchar,DT,101),Count(*))FROM EventGroup byConvert(Varchar,DT,101)
|||

SELECTCOUNT(DISTINCTDAY(DT)+' /'+MONTH(DT)+' /'+YEAR(DT))FROMEvent

Friday, March 9, 2012

Retrieving a datetime with a time of midnight (from a typical datetime)

Nothing difficult, I just need a way to generate a new datetime column based on the column [PostedDate], datetime. So basically I want to truncate the time. Thanks a lot.

A frequent method used is to (1) convert it to varchar using CONVERT with the 101 flavor and then (2) re-convert it back to datetime. Here are some examples:

Code Snippet

select convert(datetime, convert(varchar, getdate(), 101))
as dateOnly
/*
dateOnly
2007-09-07 00:00:00.000
*/

select dateadd(day, datediff (day, 0, getdate()), 0)
as dateOnly
/*
dateOnly
2007-09-07 00:00:00.000
*/

select cast(floor(cast(getdate() as float)) as datetime)
as dateOnly
/*
dateOnly
2007-09-07 00:00:00.000
*/

|||

Another way:

Code Snippet

select dateadd(d, datediff(d,0,[PostedDate]),0)

|||I used the dateadd method both of you suggested and it worked perfectly. Thank you very much.

Tuesday, February 21, 2012

retrieve Datetime

I have a table with a column tradedate of type datetime in which date
is stored in the format
mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
I tried data, time and timestamp but I do not seem to get the exact
format. Any ideas?
Thanks a lot!
No, your date is NOT stored in that format. If the column is defined as
datetime, it uses an internal format that you never see. The way it gets
displayed is determined by a number of factors, including your client
settings. If you want date to be displayed a certain way, use the CONVERT
function, and specify a format code, which is documented along with the
CONVERT function in the Books Online.
For full details on datetime storage, display and manipulation, please see:
http://www.karaszi.com/sqlserver/info_datetime.asp
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
news:1165512612.451830.77090@.79g2000cws.googlegrou ps.com...
>I have a table with a column tradedate of type datetime in which date
> is stored in the format
> mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
> I tried data, time and timestamp but I do not seem to get the exact
> format. Any ideas?
> Thanks a lot!
>
|||I had to use
convert(char(25),trade_date,131)
That was very helpful. Thank you!
Kalen Delaney wrote:[vbcol=seagreen]
> No, your date is NOT stored in that format. If the column is defined as
> datetime, it uses an internal format that you never see. The way it gets
> displayed is determined by a number of factors, including your client
> settings. If you want date to be displayed a certain way, use the CONVERT
> function, and specify a format code, which is documented along with the
> CONVERT function in the Books Online.
> For full details on datetime storage, display and manipulation, please see:
> http://www.karaszi.com/sqlserver/info_datetime.asp
>
> --
> HTH
> Kalen Delaney, SQL Server MVP
> http://sqlblog.com
>
> "db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
> news:1165512612.451830.77090@.79g2000cws.googlegrou ps.com...

retrieve Datetime

I have a table with a column tradedate of type datetime in which date
is stored in the format
mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
I tried data, time and timestamp but I do not seem to get the exact
format. Any ideas?
Thanks a lot!No, your date is NOT stored in that format. If the column is defined as
datetime, it uses an internal format that you never see. The way it gets
displayed is determined by a number of factors, including your client
settings. If you want date to be displayed a certain way, use the CONVERT
function, and specify a format code, which is documented along with the
CONVERT function in the Books Online.
For full details on datetime storage, display and manipulation, please see:
http://www.karaszi.com/sqlserver/info_datetime.asp
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
news:1165512612.451830.77090@.79g2000cws.googlegroups.com...
>I have a table with a column tradedate of type datetime in which date
> is stored in the format
> mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
> I tried data, time and timestamp but I do not seem to get the exact
> format. Any ideas?
> Thanks a lot!
>|||I had to use
convert(char(25),trade_date,131)
That was very helpful. Thank you!
Kalen Delaney wrote:
> No, your date is NOT stored in that format. If the column is defined as
> datetime, it uses an internal format that you never see. The way it gets
> displayed is determined by a number of factors, including your client
> settings. If you want date to be displayed a certain way, use the CONVERT
> function, and specify a format code, which is documented along with the
> CONVERT function in the Books Online.
> For full details on datetime storage, display and manipulation, please see:
> http://www.karaszi.com/sqlserver/info_datetime.asp
>
> --
> HTH
> Kalen Delaney, SQL Server MVP
> http://sqlblog.com
>
> "db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
> news:1165512612.451830.77090@.79g2000cws.googlegroups.com...
> >I have a table with a column tradedate of type datetime in which date
> > is stored in the format
> > mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
> > I tried data, time and timestamp but I do not seem to get the exact
> > format. Any ideas?
> >
> > Thanks a lot!
> >

retrieve Datetime

I have a table with a column tradedate of type datetime in which date
is stored in the format
mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
I tried data, time and timestamp but I do not seem to get the exact
format. Any ideas?
Thanks a lot!No, your date is NOT stored in that format. If the column is defined as
datetime, it uses an internal format that you never see. The way it gets
displayed is determined by a number of factors, including your client
settings. If you want date to be displayed a certain way, use the CONVERT
function, and specify a format code, which is documented along with the
CONVERT function in the Books Online.
For full details on datetime storage, display and manipulation, please see:
http://www.karaszi.com/sqlserver/info_datetime.asp
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
news:1165512612.451830.77090@.79g2000cws.googlegroups.com...
>I have a table with a column tradedate of type datetime in which date
> is stored in the format
> mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
> I tried data, time and timestamp but I do not seem to get the exact
> format. Any ideas?
> Thanks a lot!
>|||I had to use
convert(char(25),trade_date,131)
That was very helpful. Thank you!
Kalen Delaney wrote:[vbcol=seagreen]
> No, your date is NOT stored in that format. If the column is defined as
> datetime, it uses an internal format that you never see. The way it gets
> displayed is determined by a number of factors, including your client
> settings. If you want date to be displayed a certain way, use the CONVERT
> function, and specify a format code, which is documented along with the
> CONVERT function in the Books Online.
> For full details on datetime storage, display and manipulation, please see
:
> http://www.karaszi.com/sqlserver/info_datetime.asp
>
> --
> HTH
> Kalen Delaney, SQL Server MVP
> http://sqlblog.com
>
> "db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
> news:1165512612.451830.77090@.79g2000cws.googlegroups.com...