Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all Codeproject guys....

I have a datetime column in SQL Server 2000 that gives me data like this 10/27/2010 12:57:49 pm and I want to query this column just have return hh:mm pm like 12:57 pm. Which query I use to fetch this column. I am using this query in C# windows application.
Posted
Updated 24-Jul-13 21:35pm
v2
Comments
[no name] 20-Jul-13 14:37pm    
You don't format the data when you query it. You format the data when you display it.

In summary:
SELECT 
     CAST('2003-10-03 15:15:59. 0013621 +00:00' AS time(7)) AS 'time', 
      CAST('2003-10-03 15:15:59. 0013621 +00:00' AS date) AS 'date', 
       CAST('2003-10-03 15:15:59.153' AS smalldatetime) AS 
        'smalldatetime', 
         CAST('2003-10-03 15:15:59.153' AS datetime) AS 'datetime', 
          CAST('2003-10-03 15:15:59. 0013621 +00:00' AS datetime2(7)) AS 
        'datetime2',
           CAST('2003-10-03 15:15:59.0013621 +00:00' AS datetimeoffset(7)) AS 
        'datetimeoffset';

Pick any one, or more, of six suitable.
time date smalldatetime datetime datetime2 datetimeoffset
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15:15:59.0013621 2003-10-03 2003-10-03 15:16:00 2003-10-03 15:15:59.153 2003-10-03 15:15:59.0013621	2003-10-03 15:15:59.0013621 +00:00
 
Share this answer
 
v2
Comments
shunuman 25-Jul-13 3:25am    
It's not working, I am using sqlserver 2000.
RedDk 25-Jul-13 12:55pm    
SELECT CONVERT(varchar, GETDATE(), 109) ... DATEPART("X",GETDATE()) & replacing "X" with the usual set of abbreviations (yyyy,qq,mm,dy,dd,wk,dw,Hh,mi,ss,Ms ...)
Check below query. Convert function will do the trick

SELECT convert(varchar, date_column, 114)
from Table
 
Share this answer
 
Comments
shunuman 25-Jul-13 3:29am    
thanks for reply but it gives 14:45:00:000 and I need 2:45 PM, Please give correct query.
_Asif_ 25-Jul-13 4:11am    
SELECT CONVERT(VARCHAR(5), @date, 108) + ' ' +
SUBSTRING(CONVERT(VARCHAR(19), @date, 100),18,2)
shunuman 25-Jul-13 4:14am    
Sorry, it's not working.
shunuman 25-Jul-13 4:29am    
Table data like 7/15/2013 2:45:00 PM and I need 2:45 PM
I am getting gata from table and write it text file, my code is like..

SqlDataAdapter sdr = new SqlDataAdapter("select * from currentdata", santosh.destination);
DataSet ds = new DataSet();
sdr.Fill(ds, "currentdata");
DataTable dt = new DataTable();
dt = ds.Tables[0];
int Rowcount = dt.Rows.Count;
TextWriter twr = new StreamWriter(@"D:\TIDS.txt");
int RowPos = 0;
while (Rowcount > 0)
{
twr.WriteLine("");
twr.Write("STA=" + dt.Rows[RowPos].ItemArray[4];// I want to convert this value in 2:45 PM
RowPos++;
Rowcount--;

}
twr.Close();
}
ur,you can try to run the following codes not by convert .

SQL
select count(*) from dbo.a
where RcvDate > '2013-07-15 16:00:00.000' and RcvDate <='2013-07-16 16:00:00.000';
 
Share this answer
 
Comments
shunuman 25-Jul-13 4:16am    
Sorry, it shows Invalid column name 'RcvDate'.
Chui PuiKwan 25-Jul-13 4:43am    
question:Table data like 7/15/2013 2:45:00 PM and I need 2:45 PM ?
answer:
1.handler the data like 7/15/2013 2:45:00 PM by c#
String[] str=7/15/2013 2:45:00 PM".split(' ');
String time=str[1]; //get 2:45:00

int end =time.lastindexof(":");
time =time .substring (0,end )+str[2];

I code above codes by hand written ,so you need debug it in vs .


Chui PuiKwan 25-Jul-13 4:44am    
2.handle data by sql convert
shunuman 25-Jul-13 4:18am    
If I put my table instead of RcvDate then show 0 value.
shunuman 25-Jul-13 4:27am    
Table data like 7/15/2013 2:45:00 PM and I need 2:45 PM
I am getting gata from table and write it text file, my code is like..

SqlDataAdapter sdr = new SqlDataAdapter("select * from currentdata", santosh.destination);
DataSet ds = new DataSet();
sdr.Fill(ds, "currentdata");
DataTable dt = new DataTable();
dt = ds.Tables[0];
int Rowcount = dt.Rows.Count;
TextWriter twr = new StreamWriter(@"D:\TIDS.txt");
int RowPos = 0;
while (Rowcount > 0)
{
twr.WriteLine("");
twr.Write("STA=" + dt.Rows[RowPos].ItemArray[4];// I want to convert this value in 2:45 PM
RowPos++;
Rowcount--;

}
twr.Close();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900