Posts

Showing posts with the label sql-server-2012

Sqlcmd .bat file sql output with date append

Sqlcmd .bat file sql output with date append I have a .bat file that I have scheduled to run daily at 1am using task scheduler on a windows server 2008 r2 . The final output file gets overwritten every time so I need to add a date after the output file name. Here is the code in .bat file : code .bat SQLCMD -S ServerName -d DbName-E -i C:locscript.sql -o C:locscriptOutputtemp.txt -s"|" -W findstr /B /V /C:"----" "C:locscriptOutputtemp.txt" > "C:locfinaloutput.txt" del "C:locscriptOutputtemp.txt" I tried to add %date% like "C:locfinaloutput%date%.txt" but then nothing gets generated as output. It's my first time creating this so any guidance will be helpful. %date% "C:locfinaloutput%date%.txt" Thank you! Possible duplicate of How do I get current datetime on the Windows command line, in a suitable format for using in a filename? – Squashman Jun ...

T-SQL splitting column on a delimiter [duplicate]

T-SQL splitting column on a delimiter [duplicate] This question already has an answer here: I have a column with three groups data delimited by a forward slash, like so AB/1234/10 The column is always formatted the same in every row, with 2 characters, a slash, some number of characters, a slash, and then 2 more characters. I need to split this one column into three. So the above example becomes Column1 Column2 Column3 AB 1234 10 I'm not quite sure how to go about this. I've been using SELECT SUBSTRING but that isn't quite giving me what I need. SELECT SUBSTRING select SUBSTRING(MyColumn, 1, CHARINDEX('/', MyColumn, 1)-1) FROM MyTable Will return AB , and that's great. But I can't wrap my mind around how to grab the middle and the end sections. I thought that AB select SUBSTRING(MyColumn, 4, CHARINDEX('/', MyColumn, 4)) FROM MyTable Would work in grabbing the middle, but it returns 1234/10 1234/10 I hope my question is clear and I would appr...