---
title: "How to Write a Query to Get Records with a Specific Date Format in SQL Server?"  
description: "How to Write a Query to Get Records with a Specific Date Format in SQL Server?"  
author: "Anubhav Sharma"  
published: 2024-07-15  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160920/how-to-write-a-query-to-get-records-with-a-specific-date-format-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# How to Write a Query to Get Records with a Specific Date Format in SQL Server?

Hi everyone,

I need a [query to retrieve](https://www.mindstick.com/interview/33938/write-a-query-to-retrieve-the-total-number-of-employees-in-each-department) [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) from the `Orders` [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) where the `OrderDate` is in the format 'YYYY-MM-DD'. How can I write this query?

## Replies

### Reply by Ravi Vishwakarma

To write a [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) to [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) records with a specific date format in SQL Server, you typically use the `CONVERT` function to [convert the date column to the desired format](https://www.w3schools.com/sql/func_sqlserver_convert.asp). Here’s a general approach:

Let's assume you have a table named `YourTable` with a column named `DateColumn` that stores dates in a specific format, say 'YYYY-MM-DD'.

**SQL Server** comes with the following data types for storing a date or a date/time value in the database:

- `DATE` - format YYYY-MM-DD
- `DATETIME` - format: YYYY-MM-DD HH:MI:SS
- `SMALLDATETIME` - format: YYYY-MM-DD HH:MI:SS
- `TIMESTAMP` - format: a unique number

Here’s how you can write the query:

```plaintext
SELECT CONVERT(VARCHAR, DateOfBirth, 23)
FROM Users
```

In this query:

- `CONVERT(VARCHAR, DateColumn, 23)` converts the `DateColumn` to a `VARCHAR` (string) in format 23, which corresponds to 'YYYY-MM-DD'.
- `'YYYY-MM-DD'` is the specific format you're interested in.

Replace `YourTable` and `DateColumn` with your actual table and column names. Adjust the format code (`23` in this case) according to the specific date format you are targeting. Here are some common format codes:

- `23` - 'YYYY-MM-DD'
- `101` - 'MM/DD/YYYY'
- `102` - 'YYYY.MM.DD'
- `120` - 'YYYY-MM-DD HH:MI'

Choose the appropriate format code based on your date format. This method allows you to filter records based on a specific date format effectively in SQL Server.

Read more

[Explain the Aggregate functions in the SQL server.](https://www.mindstick.com/blog/304494/explain-the-aggregate-functions-in-the-sql-server)

[Explain the transaction in SQL Server.](https://www.mindstick.com/blog/304493/explain-the-transaction-in-sql-server)

[Explain the SQL CURSOR with an example.](https://www.mindstick.com/blog/304490/explain-the-sql-cursor-with-example)


---

Original Source: https://www.mindstick.com/forum/160920/how-to-write-a-query-to-get-records-with-a-specific-date-format-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
