How to change the date format in the SQL GETDATE() function.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi everyone,
Im trying to convert the date format MM-dd-YYYY to dd-MM-YYYY to get the age, so im using this query:
select DATEDIFF(YEAR, FECHANAC, GETDATE()) - (CASE WHEN DATEADD(YY, DATEDIFF(YEAR, FECHANAC,GETDATE()), FECHANAC) > GETDATE() THEN 1 ELSE 0 END) AS Age from TEST
Now, the FECHANAC column (varchar) has the row 21-12-2000 but the problem is that the GETDATE fuction came with 12-21-2000 date format and it fails to covert.
I also have tried using the <span style="color:#ff00ff; font-size:small convert<span style="color:#808080; font-size:small (<span style="color:#0000ff; font-size:small datetime<span style="color:#808080; font-size:small ,<span style="font-size:small <span style="color:#ff0000; font-size:small FECHANAC<span style="color:#808080; font-size:small ,<span style="font-size:small 105<span style="color:#808080; font-size:small )
or <span style="color:#ff00ff; font-size:small convert<span style="color:#808080; font-size:small (<span style="color:#0000ff; font-size:small datetime<span style="color:#808080; font-size:small ,<span style="font-size:small <span style="color:#ff0000; font-size:small GETDATE()<span style="color:#808080; font-size:small ,<span style="font-size:small 105<span style="color:#808080; font-size:small )
Like this:
select DATEDIFF(YEAR, FECHANAC, <span style="color:#ff00ff; font-size:small convert<span style="color:#808080; font-size:small (<span style="color:#0000ff; font-size:small datetime<span style="color:#808080; font-size:small ,<span style="font-size:small <span style="color:#ff0000; font-size:small GETDATE()<span style="color:#808080; font-size:small ,<span style="font-size:small 105<span style="color:#808080; font-size:small ))
- (CASE WHEN DATEADD(YY, DATEDIFF(YEAR, FECHANAC,<span style="color:#ff00ff; font-size:small convert<span style="color:#808080; font-size:small (<span style="color:#0000ff; font-size:small datetime<span style="color:#808080; font-size:small ,<span style="font-size:small <span style="color:#ff0000; font-size:small GETDATE()<span style="color:#808080; font-size:small ,<span style="font-size:small 105<span style="color:#808080; font-size:small )),
FECHANAC) > <span style="color:#ff00ff; font-size:small convert<span style="color:#808080; font-size:small (<span style="color:#0000ff; font-size:small datetime<span style="color:#808080; font-size:small ,<span style="font-size:small <span style="color:#ff0000; font-size:small GETDATE()<span style="color:#808080; font-size:small ,<span style="font-size:small 105<span style="color:#808080; font-size:small ) THEN
1 ELSE 0 END) AS Age from TEST
But i get the same error, once i got the value right, i want to display it into a dateTimePicker1in c# with this code:
<span style="white-space:pre //birthday <br/>
dateTimePicker1.Format = DateTimePickerFormat.Custom;<br/>
dateTimePicker1.CustomFormat = "dd-MM-yyyy";<br/>
dateTimePicker1.Enabled = true;<br/>
dateTimePicker1.Text = dr.GetString(3);

¿Can anyone help me with this?
Thanks!.
<br/>

View the full article
 
Back
Top