How to Extract Date From FileName using SQL statement - sql-server

I am trying to write the SQL statement to get the date from different file names. These file names might change. But, it contains date in a format of (MonthName Date YYYY) in the file name. For example, May 18 2022 in file name "AAAA May 18 2022 - User List - AAAA user list_20220518_1858.csv". The location of date in file name is not fixed. Below is an example of various file names for reference. "AAAA" is the name of company.
File Name Examples:
AAAA May 18 2022 - User List - AAAA user list_20220518_1858.csv
AAAA User Report September 1 2021 - AAAA user list.csv
November 2021 - AAAA User List - AAAA Main - User List.csv
AAAA May 18 2022 - User List - AAAA user list_20220518_1858_20220616.csv
October 2021 - AAAA User List - AAAA - Cybersource user list (4).csv
AAAA May 18 2022 - User List - AAAA user list_20220518_1858.csv
Here is what I tried for some of the file names:
DECLARE #FileName VARCHAR(100) ='AAAA User Report September 1 2021 - AAAA user list.csv'
SELECT cast(SUBSTRING(replace(#FileName, 'AAAA User Report', ''), 1, CHARINDEX('-', replace(#FileName, 'AAAA User Report', ''), 1) - 2) AS datetime) AS FileDate
AND
DECLARE #FileName VARCHAR(100) ='November 2021 - AAAA User List - AAAA Main - User List.csv'
SELECT cast(SUBSTRING(replace(#FileName, 'AAAA User List', ''), 1, CHARINDEX('-', replace(#FileName, 'AAAA User List', ''), 1) - 2) AS datetime) AS FileDate
I would like to write a single SQL statement to address all the file names irrespective of the location of the date mentioned in file names. Can someone please help/advise on regex/sql query?

Related

How to create a backup table from a table with current date & time in sql server

How to create a backup table from a table with current date & time in sql server.
The spaces in date & time should be replaced by underscore.
For Backup I am doing this :-
select * into BKP_TABLE_STUDENT
from TABLE_STUDENT
And for fetching DateTime I am using this :-
select convert(varchar, getdate(), 0)
Above gives this format -> Mar 2 2022 4:02PM
So, I need to combine the above datetime and the table name.
E.g. Table name will be BKP_TABLE_STUDENT_Mar_2_2022_4_02PM
You can conactenate your table name with a formatted string.
I would prefer a 24 hour clock than AM / PM but it's your call
SELECT CONCAT('BKP_TABLE_STUDENT_',FORMAT(getdate(),'MMM_dd_yyyy_hh_mm_tt')) as backup_name
returns
BKP_TABLE_STUDENT_Mar_02_2022_11_24_AM

sql code to get week number for FiscalYear

Need sql code to get week number for FiscalYear –
Date table ranges from 1975 to 2024
Already have weekno (WeekOfYear) for CalendarYear
-week count start from 1st of April and end on 31st march
-Week start from Sunday to Saturday
-On Fiscalyear end, week also ended
eg.
1st apr 2016 -Week 1
2nd apr 2016 - week 1
3rd apr 2016 to 9th apr 2016 -week 2
10th apr 2016 to 16th apr 2016 -week 3 ... and so on
assuming DATEFIRST is 7, I got this function - actually you can default datefirst = 7 for the lifetime of your query only - this seems to work according to by 1980 calendar
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION FiscalYearMonth
(
#DAT datetime
)
RETURNS int
AS
BEGIN
DECLARE #TAXY as int;
DECLARE #R as int;
SET #TAXY = YEAR(dateadd(month,-3,#dat));
DECLARE #TDAT as varchar(8)
SET #TDAT = CAST(#TAXY AS varchar(4)) + '0401';
SELECT #R = datediff(day, DATEADD(day , (8 - DATEPART(DW,#TDAT)) % 7,#TDAT), dateadd(day,14,#DAT)) / 7;
RETURN #R;
END
GO
e.g.
SET datefirst 7;
select dbo.FiscalYearMonth('19800413')
Writing a query to do this, seems to me very complicated. I advise you to do it either by using a table (Choice1) or by creating an EXCEL file (Choice2).
Choice1: Create a WeekCalendarForFiscalYear table with 4 fields [DateOfToday, weekNumber, FiscalYear].
In DateOfToday, you put the date of the day.
In WeekNumber, you put the number of the fiscal week.
In FiscalYear, you are in the fiscal year (2016, 2017, 2018, etc.).
You have to think about indexing these fields.
/!\ : You can also create your table like this: WeekCalendarForFiscalYear with 4 fields [WeekNumber, FiscalYear, StartDate, EndDate]. StartDate would correspond to the beginning of the fiscal week and EndDate at the end of the fiscal week.
Choix2: You create an EXCEL file in which you enter the data either as in Choix1, or as it suits you.
The file must be physically on the server of the database.
You access the file via OPENROWSET. You will find on the web a wide
range of sites that will help you to use OPENROWSET well.
Hope this can help.

yyyy/mm/dd format in SQL Server

I have a date column in SQL Server and I want to get date in yyyy/mm/dd format.
My code is:
select
convert(date, Consumption_FDate, 111)
from
dbo.TblStuff_Consumption
Consumption_FDate is my date column and dbo.TblStuff_Consumption is my table.
My problem is this code is not working; the output is same every time, though I change '111' into other numbers.
SELECT CONVERT(VARCHAR(10), Consumption_FDate, 111)
FROM dbo.TblStuff_Consumption
In SQL serve 8 working fine
select convert(varchar,getDate(),112)
CONVERT(VARCHAR(19),GETDATE()) output Nov 04 2014 11:45 PM
CONVERT(VARCHAR(10),GETDATE(),10) output 11-04-14
CONVERT(VARCHAR(10),GETDATE(),110) output 11-04-2014
CONVERT(VARCHAR(11),GETDATE(),6) output 04 Nov 14
CONVERT(VARCHAR(11),GETDATE(),106) output04 Nov 2014
CONVERT(VARCHAR(24),GETDATE(),113) output04 Nov 2014 11:45:34:24

SQL 2012 Query to output to "table" column

I have prototype of SQL query (actual query is too huge to post)
SELECT Site, Risk_Time_Stamp,COMPUTER_NAME, [IP_ADDR1_TEXT],Number_of_Risks
FROM dbo.sem_computer
WHERE [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID
GROUP BY Site, Risk_Time_Stamp,COMPUTER_NAME, [IP_ADDR1_TEXT],Number_of_Risks
That outputs
Site Risk_Time_Stamp COMPUTER_NAME IP_ADDR1_TEXT Number_of_Risks
16K987 Aug 14, 2015 ADBF8J2 10.90.0.52 2
16K987 Aug 14, 2015 AD25N10 10.51.0.80 1
16K987 Aug 14, 2015 N20C0F8J2 10.18.0.79 1
How to create query that will output site, along with column named RISK STATISTICS that has table, i.e.
SITE RISK STATISTICS
16K987 Risk_Time_Stamp COMPUTER_NAME IP_ADDR1_TEXT Number_of_Risks
Aug 14, 2015 ADBF8J2 10.90.0.52 2
Aug 14, 2015 AD25N10 10.51.0.80 1
Aug 14, 2015 N20C0F8J2 10.18.0.79 1
#sean-lange
I'm trying to create a flat excel file to input in Tableau . Each Site will be plotted on a map and if there are any risks, a hover-over will detail these.
A Site can have zero to many risks, hence the need for column with a table value, i.e. column with array value.

Formatting datetime values

I am retrieving date and time using this:
SELECT convert(varchar, getdate())
and it shows me output as:
Jul 3 2012 9:34PM
but client want that there should be space between time and AM/PM, means output should be like this:
Jul 3 2012 9:34 PM
Thanks in advance.
For one, don't use varchar without length. And you can use STUFF here, e.g.
SELECT STUFF(CONVERT(VARCHAR(19), GETDATE()), 18, 0, ' ');
Results:
Jul 3 2012 12:48 PM
Note that this can look different depending on language and regional settings.
In SQL Server 2012 this is a little more intuitive, however the extra space between month and day when the day is < 10 is tricky to work around (or my understanding of .NET's format is lacking):
SELECT FORMAT(GETDATE(), 'MMM d yyyy hh:mm tt');

Resources