How to swap day and month SQL Server - sql-server

I have a column called reading_date and it is defined as datetime. I have many records in it and I could figure out that there are many records where the day is in the month and the vise versa. For e.g 2019-05-01 which it should be 2019-01-05. is there any way to swap and fix such issue. I am using SQL Server.
Thanks in advance

Split dates using DATEPART function and gather them in another way using DATEFROMPARTS.
UPDATE yourtable
SET yourdate =
DATEFROMPARTS(
DATEPART(year, [yourdate]),
DATEPART(day, [yourdate]),
DATEPART(month, [yourdate]))
WHERE ...

Related

How to select between, for example, between 6:15 and 7:15? Microsoft SQL Server 2014

I want to query data that happened between 6:15 and 7:15, 7:15:01 and 8:15 and so forth. So far I am only able to do the following:
Select *
From table
Where datepart(hh, t_stamp) = 7
and datepart(day, t_stamp) = day(getdate())
I am selecting all data that happens between 7:00 and 7:59:59....
I tried googling it. Found something using unix_timestamp, but that does not work in Microsoft SQL Server. I've been wrecking my brain but as a SQL noob (I am used to "ladder logic" in PLC programming) this is way out of my comfort zone.
If you are looking for fetching the data between a specific time, let's say 06:15 to 07:15 for any date, then convert the datetime to time and use it in the where clause.
Query
select * from your_table_name
where cast(t_stamp as time) between '06:15:00' and '07:15:00';
If you need it only for today's date, then add that condition too in the Where clause.
select * from your_table_name
where cast(t_stamp as time) between '06:15:00' and '07:15:00'
and cast(t_stamp as date) = cast(getdate() as date);

Fixing the format of select rows in date column in a dataset

I'm working with a dataset about bakery sales.
I noticed that the date column's formatting is inconsistent.
For example, these are some of the dates as appearing on the dataset
2016-09-11
2016-09-12
2016-10-11
2016-10-12
2016-10-30
2016-10-31
2016-11-11
2016-11-12
2016-11-13
From what I've observed, the first 12 days of each month are formatted in yyyy/dd/mm, then it flips to yyyy/mm/dd. The result is that I have some false dates and some missing dates. The data should only contain dates 10/31/2016 to 04/09/2017. Is there an easy fix in SQL that I can use or is this a problem that has to be fixed in the data itself? I am using SQL Server.
I have figured it out, answering just in case in the future someone has similar issue. Thanks to #SMor for his suggestion with the case expression.
SELECT DISTINCT date,
CASE
WHEN day(date) <= 12
THEN datefromparts(datepart(year, date), day(date), datepart(month, date))
ELSE datefromparts(datepart(year, date), datepart(month, date), day(date))
END AS revised_date
FROM bakery_sales
ORDER BY revised_date

How to extract day of week from timestamp

I am trying to extract the day of the week from a timestamp in SQL Server.
I am specifically looking for the SQL Server equivalent syntax to EXTRACT.
I want to count how many fields are in each day of the week.
This is how I would do it on BigQuery:
SELECT
EXTRACT(DAYOFWEEK FROM order_date ) as day,
count(*) count_trips
FROM `sales.orders`
group by EXTRACT (DAYOFWEEK FROM order_date)
Try this:
SELECT DATENAME(WEEKDAY, DATE(timestamp))
example:
SELECT DATENAME(WEEKDAY, '2022/05/08 18:50:30');
Output: Sunday
P.S.
I am helping you the day part only considering you know the rest of your code. Feel free to reply this for exact query.

How to get date before 3 months from a particular date, say current date in SQL?

How can we deduce the date falling exactly 3 months prior to the current date in SQL Server?
use dateadd
select convert(date, dateadd(month,-3,getdate()))
output
16/07/2018 00:00:00
Use DATEADD() function
SELECT DATEADD(M, -3,GETDATE()) AS WithTime,
CAST(DATEADD(M, -3, GETDATE()) AS DATE) AS WithoutTime
If you are working on newest versions of SQL Server (2012+) I would recommand to use TRY_CONVERT() or TRY_CAST() functions.

SSIS - Modify date column from the 15th to the 1st of the month

SELECT * FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
--AND
--SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
Hello folks. Does anyone know how to correctly write this statement to
display the FIRST of each month? I have an Excel file that I am importing
into a Server using MS SSIS (VisualStudio 2008). The dates are monthly, but the last few months were the 15th June, May, April etc. My intent is to make
all of them show 1st of the month. All the months before January 2015 have been on the 1st of the month.
The SQL Query above is what I wrote in the Excel source Editor.
Thank You
This should always give you the first of the current Month
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)
to pull back the first of the month for dates in your table something like this should do it
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,[MonthlyDt]),0) FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
SELECT *, DATEADD('d',-DAY([MonthlyDT])+1, [Date]) AS [MonthD]
FROM [MarkTSK]
WHERE [MonthlyDT] IS NOT NULL
Thank you everyone. The SCRIPT above worked for me. All the dates that were not on the 1st of the month, now shows as the 1st.
Use DatePart to extract the Month and Year adding in the day manually.
Something like:
SELECT Convert(date, DatePart('yyyy', [MonthlyDt]) + DatePart('mm', [MonthlyDt]) + '01' ) FROM [MarkTSK]
WHERE [MarkTSK] IS NOT NULL
(Disclaimer: untested code)

Resources