Domanda

I've got a query that get's a date of a field in a program. This date has to be modified with 10 years.

The query I made is

SELECT DATEADD(yy, +10, '"+thisfield.value+"')

where '"+thisfield.value+"' is coming from the program and is filled in like 01-08-2012.

The result of the query is 2022-07-31 00:00:00.000. The problem I have is that I just need 2022-08-01 but in the format of 01-08-2022 so that I can automatically fill an other field with this result.

In SQL Server 2005 the date function doesn't work only the datetime function and I just don't need that.

I hope this is clear (first time i post something). Can anyone help me?

È stato utile?

Soluzione

You can either truncate the result, or cast it to DATE

CAST(DATEADD(yy, +10, '"+thisfield.value+"') AS DATE)" 

CONVERT(VARCHAR, DATEADD(yy, +10, '"+thisfield.value+"'), 101)" 

CONVERT using style 101 is in the format mm/dd/yyyy, which happens to be the format you want. Keep in mind that you can format the result however you want in your application for display purposes which is better than returning strings from SQL Server.

Also note that you should look into parameterized queries and/or stored procedures.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top