Hi i got a table with a few lines that looks like this:
id -> date
1. 2006-12-19 09:45:00
2. 2006-12-19 05:45:00
3. 2006-12-20 05:45:00
4. 2006-12-21 05:45:00Note that id 1 and 2 have the same date.
I want with a sql question get this result:
1. 2006-12-19
3. 2006-12-20
4. 2006-12-21
Is this possible, make a distinct on the date only and not the time and if, how so?
Thanks!
herman
![]() |
0 |
![]() |
Hi,
for example
SELECT DISTINCT CAST(CAST(MONTH(date) as varchar(2)) + '/' + CAST(DAY(date) as varchar(2)) + '/' + CAST(YEAR(date) as varchar(4)) as datetime) FROM TestTablewould get you distinct dates. If you need ids also, then it could lopok something like
SELECT ( SELECT top 1 id FROM testTable WHERE CAST(CAST(MONTH(date) as varchar(2)) + '/' + CAST(DAY(date) as varchar(2)) + '/' + CAST(YEAR(date) as varchar(4)) as datetime)=newdate ORDEr BY id ), newdate FROM ( SELECT DISTINCT CAST(CAST(MONTH(date) as varchar(2)) + '/' + CAST(DAY(date) as varchar(2)) + '/' + CAST(YEAR(date) as varchar(4)) as datetime) newdate FROM TestTable a ) tmpOptionally you could just use functions instead of casting on outer querySELECT ( SELECT top 1 id FROM testTable WHERE YEAR(date)=YEAR(newdate) AND MONTH(date)=MONTH(newdate) AND DAY(date)=DAY(newdate) ORDEr BY id ), newdate FROM ( SELECT DISTINCT CAST(CAST(MONTH(date) as varchar(2)) + '/' + CAST(DAY(date) as varchar(2)) + '/' + CAST(YEAR(date) as varchar(4)) as datetime) newdate FROM TestTable a ) tmp
Thanks,
Teemu Keiski
Finland, EU
![]() |
0 |
![]() |
Thanks, helped alot.
Heres another question: I want too select info from the table again but like this:
id -> date -> project
1. 2006-12-19 09:45:00 b-12
2. 2006-12-19 05:45:00 b-13
3. 2006-12-20 05:45:00 b-12
4. 2006-12-21 05:45:00 b-12SELECT project FROM table WHERE started='2006-12-19'
The result i want:
b-12
b-13But since started is a datetime it cant be done like this.
Anyone?
herman
![]() |
0 |
![]() |
hi,
try this
string co = "Data Source=localhost\\sqlexpress;Initial Catalog=pubs;Integrated Security=True";
System.Data.SqlClient.SqlConnection sqlconnect = new System.Data.SqlClient.SqlConnection(co);
System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
sqlcommand.CommandText = "select project from test where date=@date";
sqlcommand.Connection = sqlconnect;
System.Data.SqlClient.SqlParameter p1 = new System.Data.SqlClient.SqlParameter();
p1.ParameterName = "@date";
p1.Value = DateTime.Parse("12/19/2007");
p1.SqlDbType = System.Data.SqlDbType.DateTime;
sqlcommand.Parameters.Add(p1);
sqlconnect.Open();
System.Data.SqlClient.SqlDataReader dr = sqlcommand.ExecuteReader();
while (dr.Read())
TextBox1.Text = dr.GetValue(0).ToString();
sqlconnect.Close();
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
![]() |
0 |
![]() |
hi herman
you can try following
SELECT project FROM table WHERE convert(varchar,started,103) = '19/12/2006'
thanks,
satish.
Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
__________________________________________________
Please save Animals Help World Society For Protection Of Animals,
Protect these speechless creatures of GOD
![]() |
0 |
![]() |
Thanks! it worked its just, im having a datetime that i convert to dd/MM/yyyy so after the convert the result is:
dtDates.ToString("dd/MM/yyyy")
'19-12-2006'
But i need the result: '19/12/2006' -> with thease / instead of -anyone know how i can convert in a diffrent way?
herman
![]() |
0 |
![]() |
I really really need help on this one! please!!
![]() |
0 |
![]() |
/ in format string is a date separator as is replaced by the separator of your current locale. You can just write it like this
dtDates.ToString("dd") & "/" & dtDates.ToString("MM") & "/" & dtDates.ToString("yyyy")
Thanks,
Teemu Keiski
Finland, EU
![]() |
0 |
![]() |
Thank you but i had to replace the & with +, maybe because of C#?, well i duno hehhe
But it workes great now!
herman
![]() |
0 |
![]() |
Yeah C# uses + for string concatenation. I wasn't aware which language you use.
Thanks,
Teemu Keiski
Finland, EU
![]() |
0 |
![]() |
even
Response.Write(dt.ToString("dd/MM/yyyy")); worked for me.
thanks,
satish.
Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
__________________________________________________
Please save Animals Help World Society For Protection Of Animals,
Protect these speechless creatures of GOD
![]() |
0 |
![]() |