I'm trying to save data in my table for these column: year, month and date respectively. Is there a way to set these columns to adjust or update automatically to the current year, month and date?
Asked
Active
Viewed 245 times
-4
-
Do you mean to make the date columns reflect the current date when you are inserting the record in Java? – Flaom Apr 16 '19 at 06:25
-
1See [this](https://stackoverflow.com/questions/1233506/how-to-get-current-datetime-in-sql) . We usually use DB specific functions for this. – Sabir Khan Apr 16 '19 at 06:27
-
2You shouldn’t store year, month and day of month separately in three columns. Make one column of type `date` and store your date there (nearly all RDBMSs have a `date` datatype for this purpose). And yes, then most RDBMSs also offer ways to update the column value automatically. – Ole V.V. Apr 16 '19 at 06:33
-
1Why would you want to **store** the current date when that changes every 24 hours? Why don't you just include `current_date` (or the non-standard equivalent in your DBMS) in your SELECT statement? – Apr 16 '19 at 06:38
-
Yeah i think i can just insert date column and extract whatever i need from that date. Thank you all for your comments and insights. Much appreciated – Christopher Wanswett Apr 16 '19 at 10:21
1 Answers
0
You can use DATENAME
function in sql. e.g. select DATENAME(year,getdate())

Rima
- 1,447
- 1
- 6
- 12
-
1There is no function `datename()` in (standard) SQL ("SQL" is a query language - not the name of a specific database product) – Apr 16 '19 at 06:40