Skip to content
Fix Code Error

Getting only Month and Year from SQL DATE

March 13, 2021 by Code Error
Posted By: Anonymous

I need to access only Month.Year from Date field in SQL Server.

Solution

As well as the suggestions given already, there is one other possiblity I can infer from your question:
– You still want the result to be a date
– But you want to ‘discard’ the Days, Hours, etc
– Leaving a year/month only date field

SELECT
   DATEADD(MONTH, DATEDIFF(MONTH, 0, <dateField>), 0) AS [year_month_date_field]
FROM
   <your_table>

This gets the number of whole months from a base date (0) and then adds them to that base date. Thus rounding Down to the month in which the date is in.

NOTE: In SQL Server 2008, You will still have the TIME attached as 00:00:00.000
This is not exactly the same as “removing” any notation of day and time altogether.
Also the DAY set to the first. e.g. 2009-10-01 00:00:00.000

Answered By: Anonymous

Related Articles

  • MS Access SQL query - Count records until value is met
  • How to return only the Date from a SQL Server…
  • How to get the first and last date of the current year?
  • error LNK2005: ✘✘✘ already defined in…
  • Get first day of week in SQL Server
  • SQL Server Group By Month
  • Why does `this` inside filter() gets undefined in VueJS?
  • T-SQL datetime rounded to nearest minute and nearest…
  • Java 8: Difference between two LocalDateTime in…
  • How to calculate age in T-SQL with years, months, and days
  • AppCompat v7 r21 returning error in values.xml?
  • Using VBA unable to get the difference between two…
  • How can i display data from xml api in flutter?
  • Difference in Months between two dates in JavaScript
  • How do I convert datetime.timedelta to minutes,…
  • Calculate time difference in minutes in SQL Server
  • How to calculate the difference between two dates using PHP?
  • Getting the difference between two Dates…
  • R doesn't display percentages when trying to build a…
  • How to calculate age (in years) based on Date of…
  • Laravel Eloquent where field is X or null
  • TLS 1.3 server socket with Java 11 and self-signed…
  • How can I patch this Ruby module to avoid code duplication?
  • What is your most productive shortcut with Vim?
  • PostgreSQL: days/months/years between two dates
  • Hive/SQL Error when converting milliseconds to DDDD:HH:mm:ss
  • PHP + JQuery - How to use these two together? Please…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Best approach to remove time part of datetime in SQL Server
  • How to calculate the operability ratio per month on Python?
  • Best way to find the months between two dates
  • Polymer nested dom-repeat templates are not updating…
  • JavaScript function to add X months to a date
  • Sort a nested array of object when index is a key in…
  • Design DFA accepting binary strings divisible by a…
  • Tkinter Duration Timer—What method will allow this…
  • MS SQL Date Only Without Time
  • Struggling with a nested where, group by and min/max in SQL
  • Java string to date conversion
  • data.table vs dplyr: can one do something well the…
  • How to get week number of the month from the date in…
  • The definitive guide to form-based website authentication
  • Declare a variable which converts Date data type to Integer
  • How to group by month from Date field using sql
  • How to display these 5 bar plot in the same workspace?
  • Confused about the Visitor Design Pattern
  • Event listener is not working for textbox
  • SQL query return data from multiple tables
  • SQL statement to select all rows from previous day
  • Typescript Symbol.species typing inference
  • How to compare two dates to find time difference in…
  • cross join equivalent in JavaScript
  • Calculating number of full months between two dates in SQL
  • How to stop a command on only one server discord.py?
  • Polymer 1.0 'array-style' path accessors,…
  • Filtering out values from readonly tuple type
  • Compute elapsed time
  • How can I resolve Web Component Testing error?
  • Logging best practices
  • How to determine the number of days in a month in…
  • Creating list with the same number of values
  • Smart way to truncate long strings
  • Property 'dateEmail' & 'dateSigned' does not…
  • How do I calculate the date in JavaScript three…
  • Fastest way to iterate over all the chars in a String
  • sql query to find priority jobs
  • SPA best practices for authentication and session management
  • Subtracting Dates in Oracle - Number or Interval Datatype?
  • How do I obtain a Query Execution Plan in SQL Server?
  • Subtract one day from datetime
  • Can't find why this datetime test fails, in F#
  • How to query DATETIME field using only date in…
  • biblatex: splitting bibliography entry which are…
  • Why does Math.Round(2.5) return 2 instead of 3?
  • Difference in months between two dates
  • Data structure for maintaining tabular data in memory?
  • How to change the color of vaadin-select-text-field…
  • Start redis-server with config file
  • How do I get just the date when using MSSQL GetDate()?
  • How to truncate float values?
  • Usage of __slots__?
  • How to increment datetime by custom months in python…
  • Use SQL Server Management Studio to connect remotely…
  • How do I pass a unique_ptr argument to a constructor…
  • How do I count unique visitors to my site?
  • How to disable previous cloned element
  • Filtering an object based on key, then constructing…
  • How do I insert multiple checkbox values into a table?
  • Javascript return number of…
  • No 'Access-Control-Allow-Origin' header is present…
  • calculating the difference in months between two dates
  • Why does C++ code for testing the Collatz conjecture…
  • How to convert number of minutes to hh:mm format in TSQL?
  • XMLHttpRequest cannot load ✘✘✘ No…
  • Ukkonen's suffix tree algorithm in plain English
  • Remaining days calculation in MySql - Multiple maturities
  • SQL Query to find the last day of the month
  • How to get min max day in a week every month?
  • Insert into not inserting for mysql
  • JQuery function not working after another row in my…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

usr/bin/ld: cannot find -l

Next Post:

vs

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error